<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Entity Framework</title>
        <link>http://geekswithblogs.net/michelotti/category/10274.aspx</link>
        <description>Entity Framework</description>
        <language>en-US</language>
        <copyright>Steve Michelotti</copyright>
        <managingEditor>steve.michelotti@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>SOMD NUG Presentation Code Samples &amp;ndash; Building and Consuming WCF 3.5 REST Services</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/07/14/somd-nug-presentation-code-samples-ndash-building-and-consuming-wcf.aspx</link>
            <description>&lt;p&gt;Thanks to everyone who attended my presentation on WCF 3.5 REST services last night at SOMD NUG. The link to download both the code and power point is &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=michelotti&amp;amp;ReleaseId=2635" target="_blank"&gt;here&lt;/a&gt;.  It also includes the SQL script for creating the database.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=133498"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=133498" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/133498.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/07/14/somd-nug-presentation-code-samples-ndash-building-and-consuming-wcf.aspx</guid>
            <pubDate>Tue, 14 Jul 2009 16:55:54 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/133498.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/07/14/somd-nug-presentation-code-samples-ndash-building-and-consuming-wcf.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/133498.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/133498.aspx</trackback:ping>
        </item>
        <item>
            <title>Inheritance with ADO.NET Data Services and the Entity Framework</title>
            <link>http://geekswithblogs.net/michelotti/archive/2009/06/25/inheritance-with-ado.net-data-services-and-the-entity-framework.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" target="_blank"&gt;ADO.NET Data Services&lt;/a&gt; provides a robust REST API over top of a data source. That data source could be 1) the Entity Framework (EF), 2) LINQ to SQL, or 3) your own custom data source that implements &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx" target="_blank"&gt;IQueryable&lt;/a&gt; and/or &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx" target="_blank"&gt;IUpdatable&lt;/a&gt;.  However, it should be noted that in v1, EF is really the “first class” data source for ADO.NET Data Services because it supports both IQueryable and IUpdatable out of the box.  In this post I’ll discuss the scenario where you have inheritance in your entity object model and the pros and cons of the implementation. As a stand-alone ORM tool, EF has not always been extremely well received by the developer community especially in comparison to other tools such as &lt;a href="http://msdn.microsoft.com/en-us/library/bb386976.aspx" target="_blank"&gt;LINQ to SQL&lt;/a&gt; and &lt;a href="http://nhforge.org/" target="_blank"&gt;NHibernate&lt;/a&gt;.  While I generally share many of the frustrations of my fellow developers with EF, the 2 reasons I find it compelling enough to investigate further are: 1) it’s first class support for ADO.NET Data Services (and data services is great), and 2) the enhancements that are coming in EF vNext including &lt;a href="http://blogs.msdn.com/efdesign/archive/2009/06/10/code-only.aspx" target="_blank"&gt;Code Only&lt;/a&gt; with POCO which will bring EF much more in line with other ORM tools that already enable these POCO scenarios as well as &lt;a href="http://blogs.msdn.com/efdesign/archive/2008/09/10/model-first.aspx" target="_blank"&gt;Model First&lt;/a&gt; development.&lt;/p&gt;  &lt;p&gt;EF supports 3 types of inheritance: 1) Table per Hierarchy (aka Single Table Inheritance), 2) Table per Type, and 3) Table per Concrete Type.  In this example, I’ll be using the Table per Type (TPT) inheritance. I won’t cover every detail as to how I set up the TPT inheritance with EF but if you haven’t done it before then you should check out &lt;a href="http://blogs.msdn.com/bags/archive/2009/03/06/entity-framework-modeling-table-per-type-inheritance.aspx" target="_blank"&gt;this post here&lt;/a&gt; that covers step by step how to do it. In my scenario, suppose I have an EF model that looks like this:&lt;/p&gt;  &lt;p&gt; &lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_EntityDesignerDiagram.png" /&gt; &lt;/p&gt;  &lt;p&gt;Notice that I have a base class of Client which both Person and Business inherit from.  Additionally, a Person and a Business will have ClientAddresses (they inherit the addresses from the base Client class).  I add a new ADO.NET Data Service to my project which looks like this:&lt;/p&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;  &lt;div class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ClientDataService : DataService&amp;lt;ClientEntities&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeService(IDataServiceConfiguration config)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        config.SetEntitySetAccessRule(&lt;span class="str"&gt;"*"&lt;/span&gt;, EntitySetRights.All);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Note that I named my EF object context “ClientEntities” so the DataService can be strongly-typed as shown above. Also note that I’m completely opening up my permissions to all entities by using the * wildcard in the SetEntitySetAccessRule() call above but in production you’d want to think through your permissions a little more than shown in my demo code above.&lt;/p&gt;

&lt;p&gt;Now at this point, if we fire up our data service we’ll see the default meta-data screen:&lt;/p&gt;

&lt;p&gt; &lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_DefaultDSPage.PNG" /&gt; &lt;/p&gt;

&lt;p&gt;This screen shows the entity sets I have available to query but interestingly it does not show that I have a Person or Business entity set available (more on that later).  So how do I get a Person or a Business object?  I just so happen to know that in my database, the ClientID of 1 is a Person and the ClientID of 2 is a Business.  If I do a query for a Client with these respective ClientID’s, it actually figures it out for you.&lt;/p&gt;

&lt;p&gt;Here is my query for a Client who is a Person:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_ClientPerson.PNG" /&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;And here is my query for Client who is a Business:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_ClientBusiness.PNG" /&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Data Services in conjunction with EF will correctly instantiate the correct concrete type. In one regard, this is great.  But on the other hand, this isn’t very explicit.  The URI I used was: /Clients(2).  Wouldn’t it have been more explicit if I could have used the URI of /Persons(2)?  After all, that is the name of my EntitySet.  But *actually* it’s not the name of my EntitySet.  In these inheritance situations, EF allows you to specify the Entity Set Name for the base type, but it does not allow you the set the Entity Set Name for the sub-class – that just takes the same Entity Set Name as it’s base class. One way for us to use a more intuitive URI would be to create a Service Operation like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[WebGet]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; IQueryable&amp;lt;Person&amp;gt; Persons()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    var results = from c &lt;span class="kwrd"&gt;in&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.CurrentDataSource.Clients&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;                  &lt;span class="kwrd"&gt;where&lt;/span&gt; c.ClientType == &lt;span class="str"&gt;"PERSON"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;                  select c;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; results.ToList().Cast&amp;lt;Person&amp;gt;().AsQueryable();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Note you must also enable permissions to this new service operation by adding this line of code your your InitializeService() method:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;config.SetServiceOperationAccessRule(&lt;span class="str"&gt;"Persons"&lt;/span&gt;, ServiceOperationRights.All);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So now you are able to get a Person object with a more explicit and “strongly-typed” URI like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://geekswithblogs.net/images/geekswithblogs_net/michelotti/4145/o_PersonURI.PNG" /&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;This is all well and good but it does have some drawbacks.  For one thing, this service operation does not appear on the default page that shows all the meta data for the data service so the discoverability for something like this is not great.  Additionally, on the client side, the consuming developer will also have to understand these implementation details and handle the casting on their end if they’re consuming data services in the default fashion with the data services proxy.  Therefore, while I’ve found this scenario to be *possible* with Data Services and EF, I don’t really put it in the category of extremely user friendly.  However, these drawbacks seem to be more of a function of the implementation of EF than ADO.NET Data Services so I’m still a fan of data services.  It will be interesting to see how the enhancements in EF 4.0 (especially with the renewed emphasis on POCO) will change the game with scenarios like these.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=133059"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=133059" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/133059.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2009/06/25/inheritance-with-ado.net-data-services-and-the-entity-framework.aspx</guid>
            <pubDate>Fri, 26 Jun 2009 01:29:41 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/133059.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2009/06/25/inheritance-with-ado.net-data-services-and-the-entity-framework.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/133059.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/133059.aspx</trackback:ping>
        </item>
    </channel>
</rss>