The essence of a detection of key fields matter consists in
the following. When one tries to access a web service, one can receive an error
of impossibility to detect a key field or fields. To resolve these problems,
let us try to understand how ADO.NET Data Services detects key fields of the
The following algorithm is used to detect whether the fields
are key or not. At first the field named "EntityTypeID" is searched
among a set of properties ("EntityType" is the name of entity you
need). For example, if the entity is named "Customer," the key field
will be "CustomerID." If this property is found, it is a key field.
If such property is not found, the search of a field with a name "ID"
will be started. If this property is found, it is a key field. If such a property
is not found, the error will be generated.
So, let us make a small experiment to prove the reasoning.
Let us create a small table, "Customer," which contains fields
"ID" and "Name." For SQL Server scheme and LINQ-to-SQL
model see Figure 1.
Figure 1: Simple scheme of data
Let us construct data service on the basis of this model and
then we will try to refer to it. We will see that infrastructure of ADO.NET
Data Services has detected field "ID" as a key one. For the result of
access to web service see Figure 2.
Figure 2: Simple data service
Similar results can be received in the case of using field
"CustomersID" as a key one. As "CustomersID" field is a
field of the table "Customer," it will be detected as a key one.
So, let us make another experiment. Now we will create the
table "Customers," and as a key field we will use a field with the
name "CustomerID." This name of a field differs from "ID."
This name also does not meet the scheme "EntityTypeID" because
"Customer" and "Customers" have different spelling. For the
SQL Server scheme and LINQ to SQL model see Figure 3.
Figure 3: Scheme of data with custom key field
If we try to access this web service ,we will receive an
error message (see Figure 4).
Figure 4: Error message (unable to detect a key
There are two ways to solve this error. The first way is to
change the entity name to "Customer." In this case the
"CustomerID" field corresponds to the "EntityTypeID"scheme.
If entity renaming is impossible, it is possible to use DataServiceKey
attribute for the explicit setting of key fields.
DataServiceKey attribute is set for entity. All fields,
which should be key, are specified in parameters of this attribute. It is
possible to specify several fields. It is also possible to open generated code
by LINQ to SQL designer and to mark entity with this attribute. However, your
code will be destroyed when data model is changed in the designer. Therefore,
it is better to create a partial class and to mark it with attribute. The example
of such code is shown in Listing 1.
Listing 1 - The usage of DataServiceKey attribute
public partial class Customers
After "Customers" class is marked with the
DataServiceKey attribute, it can be correctly used as a data model for ADO.NET