LINQ to SQL and ADO.NET Data Services: Working Together
page 2 of 5
by Sergey Zwezdin
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27781/ 45

Detection of key fields in data model

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 entity.

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 field)

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

using System.Data.Services.Common;
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 Data Services.

View Entire Article

User Comments

Title: Mr   
Name: Nafees Khan
Date: 2012-12-05 11:36:01 PM
I need to linq simple example......
Title: Mr   
Name: Ravi Bhushan Singh(Patna)
Date: 2010-09-19 9:49:15 PM
i need to code of linq
Title: IT   
Name: Shedeep
Date: 2010-08-24 1:39:59 AM
I need to learn more code
Title: IT   
Name: chhon
Date: 2010-04-22 3:56:02 AM
I need to learn more any code such
Title: santhanakumar   
Name: kumar
Date: 2010-02-16 8:50:03 AM
yes,i want see u some of the document in development
Title: LINQ to SQL and ADO.NET Data Services: Working Together   
Name: Jagdeep Mankotia
Date: 2010-02-02 4:00:21 AM
Very good article helpful.

Covered almost all the important things.

== Jagdeep Mankotia
Title: Excellent Article   
Name: Damian
Date: 2010-01-18 2:19:21 AM
Excellent article and clear example of how to write a nice generic IUpdatable Interface. Thanks very much, just what I needed, (I was stuck)
Title: Good Article   
Name: Hamid Raza Saleemi
Date: 2009-06-16 8:51:58 PM
Good and conceptual article for Linq and addressing the lot of core issues

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-04-15 5:34:13 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search