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.

[DataServiceKey("NewsId")] 
partial class New 
{
}


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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>