{"id":391,"date":"2007-11-06T18:50:22","date_gmt":"2007-11-06T17:50:22","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?p=391"},"modified":"2007-11-06T18:50:22","modified_gmt":"2007-11-06T17:50:22","slug":"why-entity-framework-when-we-have-linq-to-sql","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/391-why-entity-framework-when-we-have-linq-to-sql.html","title":{"rendered":"Why Entity Framework when we have LINQ to SQL?"},"content":{"rendered":"<p>I&#8217;ve just returned from Carl Perry&#8217;s Tech Ed session on the Entity Framework, an object-relational library for ADO.NET, initially implemented for SQL Server. Perry is a Senior Program Manager Lead on the SQL Server team. The Entity Framework is the first implementation of what Microsoft calls the Entity Data Model. Generate a data model from a database, tweak the model in Visual Studio&#8217;s designer, then generate code to use in combination with LINQ (Language Integrated Query). I found this code snippet from Perry&#8217;s slides illuminating:<\/p>\n<blockquote>\n<pre>using (AdventureWorksModel model = new AdventureWorksModel())\n{\nvar query = from c in model.Customer\nwhere c.MiddleName == null\nselect new {\nFirstName = c.FirstName,\nLastName = c.LastName,\nEmailAddress = c.EmailAddress }; \n\nforeach (var c in query)\n {\n Response.Write(String.Format(\"&lt;p&gt;{0}\\t{1}\\t{2}&lt;\/p&gt;\",\n c.FirstName,\n c.LastName,\n c.EmailAddress));\n }\n}<\/pre>\n<\/blockquote>\n<p>In the above code, AdventureWorksModel is an instance of an Entity Framework model, and as you can see makes for clean strongly-typed coding against the database.<\/p>\n<p>But doesn&#8217;t Microsoft already have a shiny new object-relational layer called <a href=\"http:\/\/msdn2.microsoft.com\/en-gb\/library\/bb425822.aspx\" target=\"_blank\">LINQ to SQL<\/a>? Why bother with Entity Framework?<\/p>\n<p>There appears to be considerable overlap, but the Entity Framework has higher ambitions. Perry said that LINQ to SQL is fine when your entities map closely to database tables, but Entity Framework is better for more complex mappings. It is not there yet, but it looks as if Microsoft will evolve the framework to enable model-first development and add features like the ability to define constraints in the model. All very familiar in the modeling world. The question may become: why bother with LINQ to SQL?<\/p>\n<p>Entity Framework is not new; for example it is described in <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa697427(vs.80).aspx\" target=\"_blank\">this paper from 2006<\/a>. However, I had not looked at it before in any detail. You can <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=F1ADC5D1-A42E-40A6-A68C-A42EE11186F7&amp;displaylang=en\" target=\"_blank\">download a beta here<\/a>.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b3c78cec-fdd6-4e57-92cb-ad335565970e\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/entity%20framework\" rel=\"tag\">entity framework<\/a>, <a href=\"http:\/\/technorati.com\/tags\/linq%20to%20sql\" rel=\"tag\">linq to sql<\/a>, <a href=\"http:\/\/technorati.com\/tags\/linq\" rel=\"tag\">linq<\/a>, <a href=\"http:\/\/technorati.com\/tags\/sql%20server\" rel=\"tag\">sql server<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just returned from Carl Perry&#8217;s Tech Ed session on the Entity Framework, an object-relational library for ADO.NET, initially implemented for SQL Server. Perry is a Senior Program Manager Lead on the SQL Server team. The Entity Framework is the first implementation of what Microsoft calls the Entity Data Model. Generate a data model from &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/391-why-entity-framework-when-we-have-linq-to-sql.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Why Entity Framework when we have LINQ to SQL?<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=391"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/391\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}