ADO.NET Data Services and LINQ-to-SQL

As a rule, ADO.NET Data Services works over ADO.NET Entity Framework data model. However, work with real projects (and customers:)) sometimes specifies to us other conditions. Frequently there is no necessity to use Entity Framework. And in this case we start to think of use LINQ-to-SQL as data model.

In earlier versions of ADO.NET Data Services it is has been simple – create data model, create web-service and it works! When ADO.NET Data Services released, the situation has been changed – at construction of similar service based on LINQ-to-SQL and accessing to this service we see an error message.

The reasons of this problem consist in strategy of definition of the fields containing primary keys. In early versions in the absence of obvious marks of this fields, infrastructure of ADO.NET Data Services itself tried to define them. However, in release this strategy has been changed and now it is necessary to define them obvious.

To set this fields very simply. You need mark entity using DataServiceKey attribute, in constructor of which define primary keys. The information about primary keys very important for service, because when you access to single entity you should define primary key value in URL.

In what a problem in LINQ-to-SQL? Problem in that when LINQ-to-SQL generates entities, he not marks him with this attribute. Certainly, it is very annoying lack. But you can correct it very simply. As you remember, that all entities in LINQ-to-SQL data model are partial-classes. You can generate second part of partial class for each entity and mark it with DataServiceKey attribute.

partial class New 

Now ours service, which based on LINQ-to-SQL model works. Great :)


  • Hi Sergey ,

    My name is Phani and I work on the ADO.NET Data Services team.
    The convention we use to pick the identifier in the absence of the DataServiceKey attribute is :
    a) If the Entity type contains a property with the name "ID" , its the default primary key
    b) If the entity type doesnt contain a property with name "ID" , but contains a property named "EntityTypeID" , then that is the primary key.
    This convention has not changed.

    In your case , the name of the Entity Type was "New",
    the conventional rules would have picked up a property called "ID" or "NewID" from the type to be the dataservicekey. Since neither of these properties exist on your type , you have to manually define the DataServiceKey for the entity type.
    Hope this helps.

  • Thanks, Phani.
    Your information very useful. I post other article for this subject.

Comments have been disabled for this content.