<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">ADO.NET team blog</title><subtitle type="html" /><id>http://blogs.msdn.com/adonet/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/adonet/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2009-05-20T21:58:20Z</updated><entry><title>Entity Framework Provider for Synergy DBMS</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/07/01/entity-framework-provider-for-synergy-dbms.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/07/01/entity-framework-provider-for-synergy-dbms.aspx</id><published>2009-07-01T17:08:00Z</published><updated>2009-07-01T17:08:00Z</updated><content type="html">&lt;DIV class=postcontent&gt;&lt;FONT size=3 face=Calibri&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;It is my pleasure to announce that Synergex has released their Synergy/DE Data Provider for .NET with support for the Entity Framework. The Synergy/DE Data Provider for .NET enables developers to access Synergy DBMS databases from .NET applications and Visual Studio 2008 and to take advantage of the Entity Framework and LINQ. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;For more information, see the Synergex announcement page about the release &lt;A href="http://www.synergyde.com/news/article.aspx?id=3197" mce_href="http://www.synergyde.com/news/article.aspx?id=3197"&gt;here&lt;/A&gt;.&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;Congratulations to everyone at&amp;nbsp;Synergex who contributed to the release!&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;Enjoy!&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;&lt;/SPAN&gt;Elisa Flasko&lt;BR&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 13.5pt"&gt;Program Manager, Data Programmability&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9811918" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="Entity Framework" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework/default.aspx" /><category term="Providers" scheme="http://blogs.msdn.com/adonet/archive/tags/Providers/default.aspx" /><category term="Synergex" scheme="http://blogs.msdn.com/adonet/archive/tags/Synergex/default.aspx" /></entry><entry><title>Feature CTP Walkthrough: Code Only for Entity Framework</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/22/feature-ctp-walkthrough-code-only-for-entity-framework.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="7680" href="http://blogs.msdn.com/adonet/attachment/9798382.ashx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/22/feature-ctp-walkthrough-code-only-for-entity-framework.aspx</id><published>2009-06-22T21:58:00Z</published><updated>2009-06-22T21:58:00Z</updated><content type="html">&lt;FONT size=3 face=Calibri&gt;
&lt;H3&gt;Overview&lt;/H3&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;This post covers a new Entity Framework extension called Code Only. Code Only allows you to use the Entity Framework using POCO entities and without an EDMX file, i.e. with ‘Code Only’. This walkthrough shows you how to use the first release of Code Only, which is a very early and limited preview of our long term plans for Code Only. To get an idea of where Code Only is heading look at &lt;A href="http://blogs.msdn.com/efdesign/archive/2009/06/10/code-only.aspx"&gt;&lt;FONT color=#0000ff&gt;this post&lt;/FONT&gt;&lt;/A&gt; on the &lt;A href="http://blogs.msdn.com/efdesign"&gt;EF Design Blog&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;Code Only ships as part of the Microsoft Entity Framework Feature CTP 1. The CTP works on top of the latest version of the Entity Framework released as part of .NET 4.0 Beta1, and you can download it &lt;A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9" mce_href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9"&gt;here&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;SPAN style="BACKGROUND: yellow; mso-highlight: yellow"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;STRONG&gt;Requirements&lt;/STRONG&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This walkthrough requires Visual Studio 2010 Beta 1 &lt;/LI&gt;
&lt;LI&gt;The Microsoft Entity Framework Feature CTP1, that can be downloaded from &lt;A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9" mce_href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9"&gt;here&lt;/A&gt;. &lt;/LI&gt;
&lt;LI&gt;A local SQL Server 2008 Express instance has to be installed as SQLEXPRESS. Otherwise, changes to the connection string in the web.config file or regenerating the database from the included SQL scripts may be necessary. &lt;/LI&gt;
&lt;LI&gt;Download and extract the initial solution attached to this post. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx" mce_href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx"&gt;Read More…&lt;/A&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9798382" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="Entity Framework" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework/default.aspx" /><category term="Entity Framework Feature CTP1 for .NET 4.0" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP1+for+.NET+4.0/default.aspx" /></entry><entry><title>Feature CTP Walkthrough: POCO Templates for Entity Framework</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/22/feature-ctp-walkthrough-poco-templates-for-entity-framework.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="31414" href="http://blogs.msdn.com/adonet/attachment/9798363.ashx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/22/feature-ctp-walkthrough-poco-templates-for-entity-framework.aspx</id><published>2009-06-22T21:38:00Z</published><updated>2009-06-22T21:38:00Z</updated><content type="html">&lt;H3&gt;Overview&lt;/H3&gt;
&lt;P&gt;This post covers a new Entity Framework extension called POCO Templates. POCO Templates is a T4 template and associated include files, that allow you to generate POCO classes or Plain Old CLR Objects for use with the Entity Framework. T4 templates are used because T4 makes it easy to customize your Entities. &lt;/P&gt;
&lt;P&gt;POCO Templates ship as part of the Microsoft Entity Framework Feature CTP 1. The CTP works on top of the latest version of the Entity Framework released as part of .NET 4.0 Beta1, and you can download it &lt;A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9"&gt;&lt;FONT color=#4271d6&gt;here&lt;/FONT&gt;&lt;/A&gt;.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;Requirements&lt;/STRONG&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This walkthrough requires Visual Studio 2010 Beta 1 &lt;/LI&gt;
&lt;LI&gt;The Microsoft Entity Framework Feature CTP1, that can be downloaded from &lt;A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9" mce_href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9"&gt;here&lt;/A&gt;. &lt;/LI&gt;
&lt;LI&gt;A local SQL Server 2008 Express instance has to be installed as SQLEXPRESS. Otherwise, changes to the connection string in the web.config file or regenerating the database from the included SQL scripts may be necessary. &lt;/LI&gt;
&lt;LI&gt;Download and extract the initial solution attached to this post. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx" mce_href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx"&gt;Read More…&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9798363" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="Entity Framework" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework/default.aspx" /><category term="Entity Framework Feature CTP1 for .NET 4.0" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP1+for+.NET+4.0/default.aspx" /></entry><entry><title>Feature CTP Walkthrough: Self Tracking Entities for Entity Framework</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/22/feature-ctp-walkthrough-self-tracking-entities-for-entity-framework.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="392824" href="http://blogs.msdn.com/adonet/attachment/9798233.ashx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/22/feature-ctp-walkthrough-self-tracking-entities-for-entity-framework.aspx</id><published>2009-06-22T20:16:00Z</published><updated>2009-06-22T20:16:00Z</updated><content type="html">&lt;H3&gt;Overview&lt;/H3&gt;
&lt;P&gt;The goal of the following walkthrough is to demonstrate a basic scenario for the use of Self Tracking Entities with Entity Framework. Self Tracking Entities is a feature included in the Microsoft Entity Framework Community Technology Preview 1, which consists of a new API added to the ObjectContext and ObjectSet classes (implemented as extension methods for the Feature CTP) and a code-generation template based on Text Template Transformation Toolkit (T4) technology included in Visual Studio. &lt;/P&gt;
&lt;P&gt;Self Tracking Entities are domain classes that do not have any dependency on Entity Framework but include mechanisms that allow them to track changes on scalar and complex property values, references and collections within themselves, independently of the Entity Framework.&lt;/P&gt;
&lt;P&gt;The first scenario we are trying to address with Self Tracking Entities is one in which a WCF service exposes a series of operations that return entity graphs, then a client application can manipulate that graph and submit the modifications to a service operation that validates and performs the updates to a database store using Entity Framework.&lt;/P&gt;
&lt;H3&gt;Requirements&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;This walkthrough requires Visual Studio 2010 Beta 1&lt;/LI&gt;
&lt;LI&gt;The Microsoft Entity Framework Feature CTP1, that can be downloaded from &lt;A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9" mce_href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9"&gt;here&lt;/A&gt;.&lt;/LI&gt;
&lt;LI&gt;A local SQL Server 2008 Express instance has to be installed as SQLEXPRESS. Otherwise, changes to the connection string in the web.config file or regenerating the database from the included SQL scripts may be necessary.&lt;/LI&gt;
&lt;LI&gt;Download and extract the initial solution attached to this post.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx" mce_href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx"&gt;Read More...&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9798233" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="Entity Framework" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework/default.aspx" /><category term="Entity Framework Feature CTP1 for .NET 4.0" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP1+for+.NET+4.0/default.aspx" /></entry><entry><title>Announcing: Entity Framework Feature CTP 1</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/22/announcing-entity-framework-feature-ctp-1.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/22/announcing-entity-framework-feature-ctp-1.aspx</id><published>2009-06-22T19:07:00Z</published><updated>2009-06-22T19:07:00Z</updated><content type="html">&lt;P&gt;Today we are announcing the availability of the Microsoft Entity Framework Feature CTP 1 for the .NET Framework 4.0 Beta 1.&amp;nbsp; You can download the Feature CTP 1 from &lt;A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9" mce_href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=7fd7164e-9e73-43f7-90ab-5b2bf2577ac9"&gt;here&lt;/A&gt;.&amp;nbsp; This Feature CTP enables a number of scenarios that we've mentioned on the blog previously and now have a set of walk throughs for everybody to start playing with the features:&amp;nbsp; &lt;/P&gt;
&lt;P&gt;1) Better N-Tier Support with &lt;B&gt;&lt;A href="http://go.microsoft.com/fwlink/?LinkID=155219" mce_href="http://go.microsoft.com/fwlink/?LinkID=155219"&gt;Self Tracking Entities&lt;/A&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;2) POCO (Plain Old CLR Objects) entity code generation via the &lt;B&gt;&lt;A href="http://go.microsoft.com/fwlink/?LinkID=155220" mce_href="http://go.microsoft.com/fwlink/?LinkID=155220"&gt;POCO Template&lt;/A&gt; &lt;/B&gt;&lt;/P&gt;
&lt;P&gt;3) Writing only code and having it work with the Entity Framework via &lt;B&gt;&lt;A href="http://go.microsoft.com/fwlink/?LinkID=155222" mce_href="http://go.microsoft.com/fwlink/?LinkID=155222"&gt;Code Only&lt;/A&gt;&lt;/B&gt;.&lt;/P&gt;
&lt;P&gt;We weren’t able to ship these capabilities in the .NET Framework 4.0 Beta 1 so we’ve decided to release them alongside the Beta.&amp;nbsp; This CTP is an early preview of these features and as such we’re looking for lots of feedback on these components.&amp;nbsp; This functionality is currently not scheduled to be part of the .NET Framework 4.0 and we expect to release another CTP of these features based on the feedback we get from you.&amp;nbsp; Make sure to enter bugs via Microsoft Connect for the Visual Studio 2010 &amp;amp; .NET Framework 4.0 Beta located &lt;A href="https://connect.microsoft.com/VisualStudio/content/content.aspx?ContentID=12362" mce_href="https://connect.microsoft.com/VisualStudio/content/content.aspx?ContentID=12362"&gt;here&lt;/A&gt;.&amp;nbsp; You can also discuss them in the forum for the &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads" mce_href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads"&gt;Entity Framework 4.0 Beta&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Carl Perry &lt;BR&gt;Program Manager, Entity Framework&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9798160" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="Entity Framework Feature CTP1 for .NET 4.0" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP1+for+.NET+4.0/default.aspx" /></entry><entry><title>Using Repository and Unit of Work patterns with Entity Framework 4.0</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="24409" href="http://blogs.msdn.com/adonet/attachment/9763349.ashx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx</id><published>2009-06-16T23:08:00Z</published><updated>2009-06-16T23:08:00Z</updated><content type="html">&lt;P&gt;If you have been watching this blog, you know that I have been discussing the various aspects of POCO capabilities we added to Entity Framework 4.0. POCO support makes it possible to do persistence ignorance with Entity Framework in a way that was never possible with Entity Framework 3.5. &lt;/P&gt;
&lt;P&gt;If you missed the series on POCO, I’ve listed them here for your convenience. It might be a good idea to quickly check these out.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx"&gt;POCO in Entity Framework : Part 1 – The Experience&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/adonet/archive/2009/05/28/poco-in-the-entity-framework-part-2-complex-types-deferred-loading-and-explicit-loading.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/05/28/poco-in-the-entity-framework-part-2-complex-types-deferred-loading-and-explicit-loading.aspx"&gt;POCO in Entity Framework : Part 2 – Complex Types, Deferred Loading and Explicit Loading&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx"&gt;POCO in Entity Framework : Part 3 – Change Tracking with POCO&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;In this post, I’d like to look at how we might be able to take our example a bit further and use some of the common patterns such as &lt;A href="http://martinfowler.com/eaaCatalog/repository.html" mce_href="http://martinfowler.com/eaaCatalog/repository.html"&gt;Repository&lt;/A&gt; and &lt;A href="http://www.martinfowler.com/eaaCatalog/unitOfWork.html" mce_href="http://www.martinfowler.com/eaaCatalog/unitOfWork.html"&gt;Unit Of Work&lt;/A&gt; so that we can implement persistence specific concerns in our example.&lt;/P&gt;
&lt;P&gt;Expanding on our Northwind based example a bit further, let’s say I am interested in supporting the following Customer entity oriented operations:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Query for a customer by ID&lt;/LI&gt;
&lt;LI&gt;Searching for Customer by Name&lt;/LI&gt;
&lt;LI&gt;Adding a new Customer to the database&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I also want to be able to query for a product based on ID.&lt;/P&gt;
&lt;P&gt;Finally, given a customer, I would like to be able to add an Order to the database&lt;/P&gt;
&lt;P&gt;Before we get into the details, there are two things I’d like to get out of the way first:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;There is more than one “correct” way to approach this problem. I’ll likely be making many simplifying assumptions as I go – the objective is to show a very high level sketch of how you might implement the two patterns to solve the problem at hand. &lt;/LI&gt;
&lt;LI&gt;Normally, when using TDD, I’d start out with tests and use my tests to evolve my design incrementally. Since I’m not following TDD in this example, please bear with me if you see me doing things like defining an interface upfront, instead of letting tests and commonalities dictate the need for an interface, etc.&lt;/LI&gt;&lt;/UL&gt;
&lt;H4&gt;Implementing the Repository&lt;/H4&gt;
&lt;P&gt;Let’s start with the work we have to do on Customer entity and look at what a repository for dealing with Customer might look like:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt; &lt;FONT color=#51a2a2&gt;ICustomerRepository&lt;BR&gt;&lt;/FONT&gt;{        &lt;BR&gt;    &lt;FONT color=#52a3a3&gt;Customer&lt;/FONT&gt; GetCustomerById(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; id);&lt;BR&gt;    &lt;FONT color=#52a3a3&gt;IEnumerable&lt;/FONT&gt;&amp;lt;&lt;FONT color=#52a3a3&gt;Customer&lt;/FONT&gt;&amp;gt; FindByName(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; name);&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; AddCustomer(&lt;FONT color=#52a3a3&gt;Customer&lt;/FONT&gt; customer);&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;This repository interface seems to meet all the requirements around Customer:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;B&gt;GetCustomerById&lt;/B&gt; should allow me to get a single customer entity by primary key&lt;/LI&gt;
&lt;LI&gt;&lt;B&gt;FindByName&lt;/B&gt; should allow me to search for customers&lt;/LI&gt;
&lt;LI&gt;&lt;B&gt;AddCustomer&lt;/B&gt; should allow me to add customer to the database&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;This sounds good to me for the moment. Defining an interface like this for your repository is a good idea, especially if you are interested in writing tests using mocks or fakes and it allows for better unit testing by keeping your database out of the equation entirely. There are blog posts coming in the future that cover testability, mocks and fakes, etc. &lt;/P&gt;
&lt;P&gt;You might take this interface definition a bit further and define a common &lt;B&gt;IRepository&lt;/B&gt; for dealing with concerns that are common for multiple repository types. This is fine thing to do if you see that it works for you. I don’t necessarily see the need yet for this particular example and so I’ll pass on it for now. It is entirely possible that this becomes important as you add more repositories and refactor. &lt;/P&gt;
&lt;P&gt;Let’s take this repository and see how we might build an implementation of it that leverages Entity Framework to enable data access.&lt;/P&gt;
&lt;P&gt;First of all, I need an &lt;B&gt;ObjectContext&lt;/B&gt; that I can use to query for data. You might be tempted to handle &lt;B&gt;ObjectContext&lt;/B&gt; instantiation as a part of the repository’s constructor – but it is a good idea to leave that concern out of the repository and deal with that elsewhere.&lt;/P&gt;
&lt;P&gt;Here’s my constructor:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; CustomerRepository(&lt;FONT color=#4e9a9a&gt;NorthwindContext&lt;/FONT&gt; context)&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (context == &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#5eaeae&gt;ArgumentNullException&lt;/FONT&gt;(&lt;SPAN style="COLOR: #006080"&gt;&lt;FONT color=#800000&gt;"context"&lt;/FONT&gt;&lt;/SPAN&gt;);&lt;BR&gt;&lt;BR&gt;   _context = context;&lt;BR&gt;} &lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;In the above snippet, &lt;B&gt;NorthwindContext&lt;/B&gt; is my typed &lt;B&gt;ObjectContext&lt;/B&gt; type. &lt;/P&gt;
&lt;P&gt;Let’s now provide implementations for the methods required by our &lt;B&gt;ICustomerRepository&lt;/B&gt; interface. &lt;/P&gt;
&lt;P&gt;&lt;B&gt;GetCustomerById&lt;/B&gt; is trivial to implement, thanks to LINQ. Using standard LINQ operators, we can implement &lt;B&gt;GetCustomerById&lt;/B&gt; like this:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#50a0a0&gt;Customer&lt;/FONT&gt; GetCustomerById(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; id)&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; _context.Customers.Where(c =&amp;gt; c.CustomerID == id).Single();&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;Similarly, &lt;B&gt;FindByName&lt;/B&gt; could look like this. Once again, LINQ support makes this trivial to implement:&lt;INS dateTime=2009-06-15T13:27 cite="mailto:Faisal%20Mohamood"&gt; &lt;/INS&gt;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#52a3a3&gt;IEnumerable&lt;/FONT&gt;&amp;lt;&lt;FONT color=#51a2a2&gt;Customer&lt;/FONT&gt;&amp;gt; FindByName(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; name)&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; _context.Customers.Where( c =&amp;gt; c.ContactName.StartsWith(name)&lt;BR&gt;                                   ).ToList();                        &lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;Note that I chose to expose the results as &lt;B&gt;IEnumerable&lt;/B&gt;&amp;lt;&lt;I&gt;T&amp;gt;&lt;/I&gt; – you might choose to expose this as an &lt;B&gt;IQueryable&lt;/B&gt;&lt;I&gt;&amp;lt;T&amp;gt;&lt;/I&gt; instead. There are implications to doing this – in this case, I am not interested in exposing additional &lt;B&gt;IQueryable&lt;/B&gt; based query composition over what I return from my repository.&lt;/P&gt;
&lt;P&gt;And finally, let’s see how we might implement &lt;B&gt;AddCustomer&lt;/B&gt;:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; AddCustomer(&lt;FONT color=#5eaeae&gt;Customer&lt;/FONT&gt; customer)&lt;BR&gt;{&lt;BR&gt;    _context.Customers.AddObject(customer);&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;You may be tempted to also implement the save functionality as a part of the &lt;B&gt;AddCustomer&lt;/B&gt; method. While that may work for this simple example, it is generally a bad idea – this is exactly where the &lt;B&gt;Unit of Work&lt;/B&gt; comes in and we’ll see in a bit how we can use the this pattern to allow us to implement and coordinate &lt;B&gt;Save &lt;/B&gt;behavior.&lt;/P&gt;
&lt;P&gt;Here’s the complete implementation of &lt;B&gt;CustomerRepository&lt;/B&gt; that uses Entity Framework for handling persistence:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; &lt;FONT color=#53a6a6&gt;CustomerRepository&lt;/FONT&gt; : &lt;FONT color=#51a2a2&gt;ICustomerRepository&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt; &lt;FONT color=#53a6a6&gt;NorthwindContext&lt;/FONT&gt; _context;&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; CustomerRepository(&lt;FONT color=#53a6a6&gt;NorthwindContext&lt;/FONT&gt; context)&lt;BR&gt;    {&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (context == &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)&lt;BR&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#50a0a0&gt;ArgumentNullException&lt;/FONT&gt;(&lt;SPAN style="COLOR: #006080"&gt;&lt;FONT color=#800000&gt;"context"&lt;/FONT&gt;&lt;/SPAN&gt;);&lt;BR&gt;&lt;BR&gt;         _context = context;&lt;BR&gt;    }&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#50a0a0&gt;Customer&lt;/FONT&gt; GetCustomerById(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; id)&lt;BR&gt;    {&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; _context.Customers.Where(c =&amp;gt; c.CustomerID == id).Single();&lt;BR&gt;    }&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#52a3a3&gt;IEnumerable&lt;/FONT&gt;&amp;lt;&lt;FONT color=#50a0a0&gt;Customer&lt;/FONT&gt;&amp;gt; FindByName(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; name)&lt;BR&gt;    {&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; _context.Customers.Where(c =&amp;gt; c.ContactName.StartsWith(name))&lt;BR&gt;            .AsEnumerable&amp;lt;&lt;FONT color=#50a0a0&gt;Customer&lt;/FONT&gt;&amp;gt;();                        &lt;BR&gt;    }&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; AddCustomer(&lt;FONT color=#50a0a0&gt;Customer&lt;/FONT&gt;customer)&lt;BR&gt;    {&lt;BR&gt;        _context.Customers.AddObject(customer);&lt;BR&gt;    }&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;Here’s how we might use the repository from client code:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;FONT color=#51a2a2&gt;CustomerRepository&lt;/FONT&gt; repository = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#51a2a2&gt;CustomerRepository&lt;/FONT&gt;(context);&lt;BR&gt;&lt;FONT color=#53a6a6&gt;Customer&lt;/FONT&gt; c = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#51a2a2&gt;Customer&lt;/FONT&gt;( ... );&lt;BR&gt;repository.AddCustomer(c);&lt;BR&gt;context.SaveChanges();&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;For dealing with my &lt;B&gt;Product&lt;/B&gt; and &lt;B&gt;Order&lt;/B&gt; related requirements, I could define the following interfaces (and build implementations much like &lt;B&gt;CustomerRepository&lt;/B&gt;). I’ll leave the details out of this post for brevity.&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt; &lt;FONT color=#54a9a9&gt;IProductRepository&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;    &lt;FONT color=#5aadad&gt;Product &lt;/FONT&gt;GetProductById(&lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; id);&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt; &lt;FONT color=#58abab&gt;IOrderRepository&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; AddOrder(&lt;FONT color=#54a9a9&gt;Order&lt;/FONT&gt; order);&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;H4&gt;Implementing Unit of Work Pattern using ObjectContext&lt;/H4&gt;
&lt;P&gt;You may have noticed this already; even though we didn’t implement any specific pattern to explicitly allow us to group related operations into a unit of work, we are already getting &lt;B&gt;Unit of Work&lt;/B&gt; functionality for free with &lt;B&gt;NorthwindContext&lt;/B&gt; (our typed &lt;B&gt;ObjectContext&lt;/B&gt;). &lt;/P&gt;
&lt;P&gt;The idea is that I can use the Unit of Work to group a set of related operations – the &lt;B&gt;Unit of Work&lt;/B&gt; keeps track of the changes that I am interested in until I am ready to save them to the database. Eventually, when I am ready to save, I can do that.&lt;/P&gt;
&lt;P&gt;I can define an interface like this to define a “Unit of Work”:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;interface&lt;/SPAN&gt; &lt;FONT color=#53a6a6&gt;IUnitOfWork&lt;BR&gt;&lt;/FONT&gt;{        &lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Save();&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;Note that with a Unit of Work, you might also choose to implement Undo / Rollback functionality. When using Entity Framework, the recommended approach to undo is to discard your context with the changes you are interested in undoing.&lt;/P&gt;
&lt;P&gt;I already mentioned that our typed ObjectContext (NorthwindContext) supports the &lt;B&gt;Unit of Work&lt;/B&gt; pattern for the most part. In order to make things a bit more explicit based on the contract I just defined, I can change my NorthwindContext class to implement the &lt;B&gt;IUnitOfWork&lt;/B&gt; interface:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; &lt;FONT color=#54a9a9&gt;NorthwindContext &lt;FONT color=#000000&gt;:&lt;/FONT&gt; ObjectContext, IUnitOfWork&lt;/FONT&gt;&lt;BR&gt;{   &lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Save()&lt;BR&gt;    {&lt;BR&gt;        SaveChanges();&lt;BR&gt;    }&lt;BR&gt;&lt;BR&gt;. . . &lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;I have to make a small adjustment to our repository implementation after this change:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; &lt;FONT color=#51a2a2&gt;CustomerRepository&lt;/FONT&gt; : &lt;FONT color=#59acac&gt;ICustomerRepository&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;private&lt;/SPAN&gt; &lt;FONT color=#58abab&gt;NorthwindContext &lt;/FONT&gt;_context;&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; CustomerRepository(&lt;FONT color=#4e9c9c&gt;IUnitOfWork&lt;/FONT&gt; unitOfWork)&lt;BR&gt;    {&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (unitOfWork == &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;)&lt;BR&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#4e9c9c&gt;ArgumentNullException&lt;/FONT&gt;(&lt;SPAN style="COLOR: #006080"&gt;&lt;FONT color=#800000&gt;"unitOfWork"&lt;/FONT&gt;&lt;/SPAN&gt;);&lt;BR&gt;&lt;BR&gt;        _context = unitOfWork &lt;SPAN style="COLOR: #0000ff"&gt;as&lt;/SPAN&gt; &lt;FONT color=#58abab&gt;NorthwindContext&lt;/FONT&gt;;&lt;BR&gt;    }&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#58abab&gt;Customer &lt;/FONT&gt;GetCustomerById(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; id)&lt;BR&gt;    {&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; _context.Customers.Where(c =&amp;gt; c.CustomerID == id).Single();&lt;BR&gt;    }&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#50a0a0&gt;IEnumerable&lt;/FONT&gt;&amp;lt;&lt;FONT color=#58abab&gt;Customer&lt;/FONT&gt;&amp;gt; FindByName(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; name)&lt;BR&gt;    {&lt;BR&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; _context.Customers.Where(c =&amp;gt; c.ContactName.StartsWith(name))&lt;BR&gt;            .AsEnumerable&amp;lt;&lt;FONT color=#58abab&gt;Customer&lt;/FONT&gt;&amp;gt;();&lt;BR&gt;    }&lt;BR&gt;&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; AddCustomer(&lt;FONT color=#58abab&gt;Customer&lt;/FONT&gt; customer)&lt;BR&gt;    {&lt;BR&gt;        _context.Customers.AddObject(customer);&lt;BR&gt;    }&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That’s it – we now have our &lt;B&gt;IUnitOfWork&lt;/B&gt; friendly repository, and you can use the &lt;B&gt;IUnitOfWork&lt;/B&gt; based context to even coordinate work across multiple repositories. Here’s an example of adding an order to the database that requires the work of multiple repositories for querying data, and ultimately saving rows back to the database:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;FONT color=#54a7a7&gt;IUnitOfWork&lt;/FONT&gt; unitOfWork = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#4f9d9d&gt;NorthwindContext&lt;/FONT&gt;();&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#52a3a3&gt;CustomerRepository&lt;/FONT&gt; customerRepository = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#52a3a3&gt;CustomerRepository&lt;/FONT&gt;(unitOfWork);&lt;BR&gt;&lt;FONT color=#58abab&gt;Customer&lt;/FONT&gt; customer = customerRepository.GetCustomerById(&lt;SPAN style="COLOR: #006080"&gt;&lt;FONT color=#800000&gt;"ALFKI"&lt;/FONT&gt;&lt;/SPAN&gt;);&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#4e9c9c&gt;ProductRepository&lt;/FONT&gt; productRepository = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#4e9c9c&gt;ProductRepository&lt;/FONT&gt;(unitOfWork);&lt;BR&gt;&lt;FONT color=#51a2a2&gt;Product&lt;/FONT&gt; product = productRepository.GetById(1);&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#52a3a3&gt;OrderRepository&lt;/FONT&gt; orderRepository = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#52a3a3&gt;OrderRepository&lt;/FONT&gt;(unitOfWork);&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#53a6a6&gt;Order&lt;/FONT&gt; order = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; &lt;FONT color=#53a6a6&gt;Order&lt;/FONT&gt;(customer); &lt;BR&gt;order.AddNewOrderDetail(product, 1);&lt;BR&gt;            &lt;BR&gt;orderRepository.AddOrder(order);&lt;BR&gt;&lt;BR&gt;unitOfWork.Save();&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;What’s quite interesting to see is how little code we had to write in order to enable data access using Repository and Unit of Work patterns on top of Entity Framework. Entity Framework’s LINQ support and out of the box &lt;B&gt;Unit of Work&lt;/B&gt; functionality makes it trivial to build repositories on top of Entity Framework.&lt;/P&gt;
&lt;P&gt;Another thing to note is that a lot of what I’ve covered in this post has nothing to do with Entity Framework 4.0 specifically – all of these general principles will work with Entity Framework 3.5 as well. But being able to use &lt;B&gt;Repository&lt;/B&gt; and &lt;B&gt;Unit of Work&lt;/B&gt; like I have shown here on top of our POCO support is really telling of what’s possible in Entity Framework 4.0. I hope you find this useful.&lt;/P&gt;
&lt;P&gt;Lastly, I’ll reiterate that there are many ways of approaching this topic, and there are many variations of solutions that will fit your needs when working with Entity Framework, Repository and Unit of Work. You might choose to implement a common &lt;B&gt;IRepository&lt;/B&gt; interface. You might also choose to implement a &lt;B&gt;Repository&lt;/B&gt;&amp;lt;&lt;I&gt;TEntity&lt;/I&gt;&amp;gt; base class that gives you some common repository functionality across all your repositories. &lt;/P&gt;
&lt;P&gt;Try some of the various approaches and see what works for you. What I’ve covered here is only a general guideline – but I hope it is enough to get you going. More importantly, I hope this shows you how it is possible to use the POCO support we have introduced in Entity Framework 4.0 to help you build a domain model that is Entity Framework friendly without needing you to compromise on the basic principles around persistence ignorance.&lt;/P&gt;
&lt;P&gt;The project that includes some of the code I showed is attached to this post. Stay tuned as we’ll likely have more to say on this topic when we discuss unit testing, TDD and testability in one of our future posts.&lt;/P&gt;
&lt;P&gt;Faisal Mohamood &lt;BR&gt;Program Manager, Entity Framework&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9763349" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author></entry><entry><title>System.Data.OracleClient Update</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/15/system-data-oracleclient-update.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/15/system-data-oracleclient-update.aspx</id><published>2009-06-15T18:59:54Z</published><updated>2009-06-15T18:59:54Z</updated><content type="html">&lt;p&gt;As a part of formulating our long term strategy for ADO.NET, we have had several discussions with number of our customers, internal and external partners, and MVPs to better align our development efforts to ensure we are delivering the right technologies according to our customers’ highest priority needs. One of the key intent of these discussions and the associated research was to understand the needs and requirements of customers who develop applications with Oracle using “System.Data.OracleClient” (OracleClient).&amp;#160;&amp;#160; OracleClient is the ADO.NET provider for Oracle developed by Microsoft and ships as a part of the .NET Framework.&lt;/p&gt;  &lt;p&gt;We learned&amp;#160; that a significantly large portion of customers use our partners’&amp;#160; ADO.NET providers for Oracle;&amp;#160; with regularly updated support for Oracle releases and new features. In addition, many of the third party providers are able to consistently provide the same level of quality and support that customers have come to expect from Microsoft. This is strong testament of our partners support for our technologies and the strength of our partner ecosystem.&amp;#160; It is our assessment that even if we made significant investments in ADO.Net OracleClient to bring it at parity with our partners based providers, customers would not have a compelling reason to switch to ADO.Net OracleClient.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;The Decision&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;After carefully considering all the options and talking to our customers, partners, and MVPs it was decided to deprecate OracleClient as a part of our ADO.NET roadmap.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Recommendation and Guidance:&amp;#160; &lt;/b&gt;&lt;/p&gt;  &lt;p&gt;System.Data.OracleClient will be available in the upcoming 4.0 release of .NET Framework; however, it will be marked as deprecated. This will have no impact to existing applications and these applications will continue to work as expected. Developing new applications which use OracleClient will be supported; however, warnings will be raised if the applications are compiled against .Net 4.0. Once compiled, no warnings or errors will be generated while running these applications. We strongly recommend customers to use&amp;#160; our partners’ ADO.NET Provider for Oracle&amp;#160; instead of continuing to use Microsoft’s OracleClient for new application development.&lt;/p&gt;  &lt;p&gt;Microsoft will continue to provide hotfixes for critical issues in System.Data.OracleClient as per the standard support policy for .Net Framework 4.0. We will also continue to make critical bug fixes in future service packs for .Net Framework 4.0.&lt;/p&gt;  &lt;p&gt;Thank you, &lt;/p&gt;  &lt;p&gt;Himanshu Vasishth   &lt;br /&gt;Program Manager, ADO.NET OracleClient&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9754432" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author></entry><entry><title>POCO in the Entity Framework : Part 3 – Change Tracking with POCO</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx</id><published>2009-06-10T15:43:05Z</published><updated>2009-06-10T15:43:05Z</updated><content type="html">&lt;p&gt;In my &lt;a href="http://blogs.msdn.com/adonet/archive/2009/05/28/poco-in-the-entity-framework-part-2-complex-types-deferred-loading-and-explicit-loading.aspx"&gt;last post on POCO&lt;/a&gt;, I mentioned the fact that there are two types of change tracking possibilities with POCO: &lt;b&gt;Snapshot based Change Tracking&lt;/b&gt; and &lt;b&gt;Notification based Change Tracking with Proxies.&lt;/b&gt; In this post, I would like to drill into both options a bit further and cover the advantages and disadvantages of both, along with the implications of using either approach. We will also be posting ideas on the &lt;a href="http://blogs.msdn.com/efdesign/"&gt;EF Design Blog&lt;/a&gt; regarding some of the feedback we have received so far about POCO support in Entity Framework 4.0.&lt;/p&gt;  &lt;h5&gt;Snapshot based Change Tracking (pure POCO without proxies)&lt;/h5&gt;  &lt;p&gt;As I mentioned in Part 2 of this series, Snapshot based change tracking is what you get with pure POCO entities that don’t use proxies to handle change tracking. This is a simple change tracking solution that relies on complete snapshots of before and after values being maintained by the Entity Framework. These values are compared during &lt;b&gt;SaveChanges&lt;/b&gt; to determine what has truly changed from their original values. In this model, unless you are using Lazy loading, the runtime type of your entities is the same as you define for your POCO entities.&lt;/p&gt;  &lt;p&gt;There really isn’t anything problematic with this approach, and if you care about the runtime purity of your entity types without the use of proxies, it is perfectly fine to rely on this approach and take the route of pure POCO entities that don’t rely on proxy types for additional functionality. &lt;/p&gt;  &lt;p&gt;The only catch with Snapshot based change tracking is that there are a few things you have to be aware of: because there is no direct notification of changes to the Entity Framework anytime your objects change, Entity Framework’s Object State manager will go out of sync with your own object graph.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Let’s take an example to see how this manifests itself:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;font color="#4b9696"&gt;Customer&lt;/font&gt; customer = (&lt;font color="#0000ff"&gt;from&lt;/font&gt; c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; context.Customers&lt;br /&gt;                     &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; c.CustomerID == &lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;ALFKI&amp;quot;&lt;/font&gt;&lt;/span&gt;&lt;br /&gt;                     &lt;font color="#0000ff"&gt;select&lt;/font&gt; c).Single();&lt;br /&gt;&lt;br /&gt;&lt;font color="#4e9c9c"&gt;ObjectStateEntry&lt;/font&gt; ose = context.ObjectStateManager.GetObjectStateEntry(customer);&lt;br /&gt;&lt;br /&gt;&lt;font color="#51a2a2"&gt;Console&lt;/font&gt;.WriteLine(&lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;Customer object state: {0}&amp;quot;&lt;/font&gt;&lt;/span&gt;, ose.State); &lt;span style="color: #008000"&gt;// Unchanged&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;customer.Country = &lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;UK&amp;quot;&lt;/font&gt;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;font color="#4f9d9d"&gt;Console&lt;/font&gt;.WriteLine(&lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;Customer object state: {0}&amp;quot;&lt;/font&gt;&lt;/span&gt;, ose.State); &lt;span style="color: #008000"&gt;// Still Unchanged&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;In this example, Customer is a pure POCO type. Unlike with &lt;b&gt;EntityObject&lt;/b&gt; or &lt;b&gt;IPOCO&lt;/b&gt; based entities, making changes to the entity doesn’t automatically keep the state manager in sync because there is no automatic notification between your pure POCO entities and the Entity Framework. Therefore, upon querying the state manager, it thinks that the customer object state is &lt;b&gt;Unchanged&lt;/b&gt; even though we have explicitly made a change to one of the properties on the entity.&lt;/p&gt;

&lt;p&gt;If you were to perform &lt;b&gt;SaveChanges &lt;/b&gt;without choosing to &lt;b&gt;acceptChangesDuringSave&lt;/b&gt;, you will see that the state becomes &lt;b&gt;Modified&lt;/b&gt; after the save. This is because the snapshot based change tracking kicked in during save and detected the change. Of course, the default &lt;b&gt;SaveChanges&lt;/b&gt; call will leave the state back to &lt;b&gt;Unchanged&lt;/b&gt; since the default Save behavior is to accept changes during save.&lt;/p&gt;

&lt;p&gt;We’ll discuss more about what to do when you would actually need to keep things in sync between your objects, the object graph and the state manager in a little bit. But first let’s look at the other type of change tracking that’s available to you with POCO.&lt;/p&gt;

&lt;h5&gt;Notification based Change Tracking with Proxies&lt;/h5&gt;

&lt;p&gt;This is a different solution if you care about very efficient and up to date change tracking as you make changes to your entity values, relationships and object graph: &lt;b&gt;Proxy based Change Tracking&lt;/b&gt;. You can leverage proxy based change tracking for a particular entity type if you declare all mapped properties on that entity type as &lt;b&gt;virtual.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Entities that are tracked using change tracking proxies will always be in sync with the Entity Framework’s Object state manager because the proxies will notify the Entity Framework of the changes to values and relationships as each change happens. On the whole, this makes it more efficient to track changes because the object state manager can skip comparing original and current values of properties that it knows haven’t changed. &lt;/p&gt;

&lt;p&gt;For all intents and purposes, the change tracking behavior you get with proxies is identical to the change tracking behavior you get with &lt;b&gt;EntityObject&lt;/b&gt; based non-POCO entities or IPOCO entities.&lt;/p&gt;

&lt;p&gt;Let’s take the same example:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;font color="#4f9d9d"&gt;Customer&lt;/font&gt; customer = (&lt;font color="#0000ff"&gt;from&lt;/font&gt; c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; context.Customers&lt;br /&gt;                     &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; c.CustomerID == &lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;ALFKI&amp;quot;&lt;/font&gt;&lt;/span&gt;&lt;br /&gt;                     &lt;font color="#0000ff"&gt;select&lt;/font&gt; c).Single();&lt;br /&gt;&lt;br /&gt;&lt;font color="#51a2a2"&gt;ObjectStateEntry&lt;/font&gt; ose = context.ObjectStateManager.GetObjectStateEntry(customer);&lt;br /&gt;&lt;br /&gt;&lt;font color="#4d9999"&gt;Console&lt;/font&gt;.WriteLine(&lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;Customer object state: {0}&amp;quot;&lt;/font&gt;&lt;/span&gt;, ose.State); &lt;span style="color: #008000"&gt;// Unchanged&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;customer.Country = &lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;UK&amp;quot;&lt;/font&gt;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;font color="#4d9999"&gt;Console&lt;/font&gt;.WriteLine(&lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;Customer object state: {0}&amp;quot;&lt;/font&gt;&lt;/span&gt;, ose.State); &lt;span style="color: #008000"&gt;// Modified&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The example is self explanatory – Object State Manager was notified of the changes as you made changes to your entity. There is no additional overhead you have to incur during &lt;b&gt;SaveChanges&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;Proxy based change tracking does however mean that the runtime type of your entities is not exactly the same type you defined – rather, it is a subclass of your own type. This can be undesirable in many cases (such as serialization) and you have to choose the approach that best works within the constraints and the requirements of your application and domain.&lt;/p&gt;

&lt;h4&gt;Keeping the State Manager in Sync without using Proxies&lt;/h4&gt;

&lt;p&gt;There are pros and cons to both approaches and it certainly won’t be surprising that the simplicity and elegance of non proxy based pure POCO will mean that it becomes the default choice for many of you. So let’s look at how you can keep the state manager in sync with your object graph when you work in this mode.&lt;/p&gt;

&lt;h5&gt;ObjectContext.DetectChanges()&lt;/h5&gt;

&lt;p&gt;There is one method that matters the most when you work with snapshot based pure POCO entities: &lt;b&gt;ObjectContext.DetectChanges()&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;This API should be used explicitly whenever you have made changes to your object graph, and it will inform the state manager that it needs to sync up with your graph. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;ObjectContext.SaveChanges &lt;/b&gt;will implicitly invoke &lt;b&gt;DetectChanges&lt;/b&gt; by default, so you don’t need to explicitly call &lt;b&gt;DetectChanges&lt;/b&gt; if all you are doing is making a series of changes to your objects and immediately performing a Save. However, keep in mind that depending on the size of your graph, &lt;b&gt;DetectChanges&lt;/b&gt; may turn out to be expensive (also &lt;b&gt;DetectChanges&lt;/b&gt; might be unnecessary depending on what you are doing) and therefore it is possible for you to skip &lt;b&gt;DetectChanges&lt;/b&gt; that happens implicitly during &lt;b&gt;SaveChanges&lt;/b&gt; - more on this when we discuss &lt;b&gt;SaveChanges&lt;/b&gt; additions that are new to Entity Framework 4.0.&lt;/p&gt;

&lt;p&gt;Let’s take a look at how &lt;b&gt;DetectChanges&lt;/b&gt; impacts our example we looked at earlier:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;font color="#4d9999"&gt;Customer&lt;/font&gt; customer = (&lt;font color="#0000ff"&gt;from&lt;/font&gt; c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; context.Customers&lt;br /&gt;                     &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; c.CustomerID == &lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;ALFKI&amp;quot;&lt;/font&gt;&lt;/span&gt;&lt;br /&gt;                     &lt;font color="#0000ff"&gt;select&lt;/font&gt; c).Single();&lt;br /&gt;&lt;br /&gt;ObjectStateEntry ose = context.ObjectStateManager.GetObjectStateEntry(customer);&lt;br /&gt;&lt;br /&gt;&lt;font color="#4d9999"&gt;Console&lt;/font&gt;.WriteLine(&lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;Customer object state: {0}&amp;quot;&lt;/font&gt;&lt;/span&gt;, ose.State); &lt;span style="color: #008000"&gt;// Unchanged&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;customer.Country = &lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;UK&amp;quot;&lt;/font&gt;&lt;/span&gt;;&lt;br /&gt;&lt;font color="#4d9999"&gt;Console&lt;/font&gt;.WriteLine(&lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;Customer object state: {0}&amp;quot;&lt;/font&gt;&lt;/span&gt;, ose.State); &lt;span style="color: #008000"&gt;// Still Unchanged&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;context.DetectChanges();&lt;br /&gt;&lt;font color="#4d9999"&gt;Console&lt;/font&gt;.WriteLine(&lt;span style="color: #006080"&gt;&lt;font color="#800000"&gt;&amp;quot;Customer object state: {0}&amp;quot;&lt;/font&gt;&lt;/span&gt;, ose.State); &lt;span style="color: #008000"&gt;// Modified&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Calling &lt;b&gt;DetectChanges &lt;/b&gt;explicitly brings the state manager in sync with the state of your objects.&lt;/p&gt;

&lt;p&gt;Because &lt;b&gt;DetectChanges&lt;/b&gt; is potentially expensive, it isn’t called explicitly during the other APIs on ObjectContext that rely on the state manager being in sync with the object graph. As a result, you have to call &lt;b&gt;DetectChanges&lt;/b&gt; whenever you are performing state dependent operations on the context.&lt;/p&gt;

&lt;p&gt;The behavior of the following APIs in Object Servces depend on the current state in the ObjectStateManager and therefore could be affected if knowledge aobut the part of the graph on which they operate is out of sync with the actual state of the graph:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;ObjectContext API: &lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AddObject&lt;/li&gt;

  &lt;li&gt;Attach&lt;/li&gt;

  &lt;li&gt;AttachTo&lt;/li&gt;

  &lt;li&gt;DeleteObject&lt;/li&gt;

  &lt;li&gt;Detach&lt;/li&gt;

  &lt;li&gt;GetObjectByKey&lt;/li&gt;

  &lt;li&gt;TryGetObjectByKey&lt;/li&gt;

  &lt;li&gt;ApplyCurrentValues &lt;/li&gt;

  &lt;li&gt;ApplyPropertyChanges&lt;/li&gt;

  &lt;li&gt;ApplyOriginalValues&lt;/li&gt;

  &lt;li&gt;Refresh&lt;/li&gt;

  &lt;li&gt;ExecuteQuery &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;ObjectStateManager API:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ChangeObjectState&lt;/li&gt;

  &lt;li&gt;ChangeRelationshipState&lt;/li&gt;

  &lt;li&gt;GetObjectStateEntry&lt;/li&gt;

  &lt;li&gt;TryGetObjectStateEntry&lt;/li&gt;

  &lt;li&gt;GetObjectStateEntries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;ObjectStateEntry API:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Any&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;EntityCollection/EntityReference API:&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Any&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;New Overloads for SaveChanges&lt;/h4&gt;

&lt;p&gt;In Entity Framework 3.5, there were two possibilities when invoking &lt;b&gt;ObjectContext.SaveChanges()&lt;/b&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;SaveChanges()&lt;/b&gt; – The parameterless option allowed you to save changes, and also accept changes implicitly&lt;/li&gt;

  &lt;li&gt;&lt;b&gt;SaveChanges(&lt;/b&gt;bool&lt;b&gt; acceptChangesDuringSave) &lt;/b&gt;– This option allowed you to opt out of the default behavior of “accept changes during save”. You will have to explicitly use &lt;b&gt;AcceptAllChanges&lt;/b&gt; to accept changes after the save.&lt;b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With &lt;b&gt;DetectChanges &lt;/b&gt;behavior added to the mix, it was obvious that adding additional overloads would clutter the API. In order to address this problem, the &lt;b&gt;SaveOptions&lt;/b&gt; flags enum was introduced:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[&lt;font color="#489191"&gt;Flags&lt;/font&gt;]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;enum&lt;/span&gt; &lt;font color="#51a2a2"&gt;SaveOptions&lt;/font&gt;&lt;br /&gt;{&lt;br /&gt;    None = 0,&lt;br /&gt;    DetectChangesBeforeSave = 1,&lt;br /&gt;    AcceptChangesAfterSave = 2,&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;SaveChanges&lt;/b&gt; now includes an overload that looks like this:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; SaveChanges(&lt;font color="#4f9d9d"&gt;SaveOptions&lt;/font&gt; options);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;SaveOptions&lt;/b&gt; will allow us (the Entity Framework team) to extend the API in a much more manageable way should we need to add additional options in the future. &lt;/p&gt;

&lt;p&gt;But this overload is also interesting for another reason: Note that this overload of &lt;b&gt;SaveChanges&lt;/b&gt; is virtual. In Entity Framework 4.0, you can override the &lt;b&gt;SaveChanges&lt;/b&gt; behavior with your own behavior customization, as a part of the ObjectContext derived implementations that you might choose to write. Whether or not you are using POCO support in EF, this will allow you add customizations in a way that was not possible with Entity Framework 3.5.&lt;/p&gt;

&lt;p&gt;That pretty much wraps up most of the things I wanted to highlight about POCO in Entity Framework 4.0. Stay tuned for a write-up on using Patterns with Entity Framework next.&lt;/p&gt;

&lt;p&gt;Faisal Mohamood
  &lt;br /&gt;Program Manager, Entity Framework Team&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9724468" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author></entry><entry><title>ADO.NET Entity Framework Provider for Firebird RTM Now Available</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/06/01/ado-net-entity-framework-provider-for-firebird-rtm-now-available.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/06/01/ado-net-entity-framework-provider-for-firebird-rtm-now-available.aspx</id><published>2009-06-01T16:29:00Z</published><updated>2009-06-01T16:29:00Z</updated><content type="html">&lt;P&gt;Firebird recently announced the RTM of FirebirdClient 2.5. This new release now provides full support for the Entity Framework. To learn more about FirebirdClient 2.5 check out Jiri’s blog at&amp;nbsp;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;A href="http://blog.vyvojar.cz/jirka/archive/2009/05/22/ann-firebirdclient-2-5-final-released.aspx"&gt;&lt;FONT color=#0000ff&gt;http://blog.vyvojar.cz/jirka/archive/2009/05/22/ann-firebirdclient-2-5-final-released.aspx&lt;/FONT&gt;&lt;/A&gt;&lt;/SPAN&gt; or access the download page at&amp;nbsp; &lt;A title=http://www.firebirdsql.org/index.php?op=files&amp;amp;id=netprovider href="http://www.firebirdsql.org/index.php?op=files&amp;amp;id=netprovider" mce_href="http://www.firebirdsql.org/index.php?op=files&amp;amp;id=netprovider"&gt;http://www.firebirdsql.org/index.php?op=files&amp;amp;id=netprovider&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Elisa Flasko &lt;BR&gt;Program Manager, Data Programmability&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9678463" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="Providers" scheme="http://blogs.msdn.com/adonet/archive/tags/Providers/default.aspx" /><category term="Firebird" scheme="http://blogs.msdn.com/adonet/archive/tags/Firebird/default.aspx" /></entry><entry><title>Entity Framework VB Samples Now Available on CodeGallery!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/05/29/entity-framework-vb-samples-now-available-on-codegallery.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/05/29/entity-framework-vb-samples-now-available-on-codegallery.aspx</id><published>2009-05-29T16:33:08Z</published><updated>2009-05-29T16:33:08Z</updated><content type="html">&lt;p&gt;This week the team was able to make available in VB, 7 of our Entity Framework Samples that were previously only available in C#. We would like to say a huge THANK YOU to one of our wonderful VB MVPs, &lt;a href="http://www.thinqlinq.com/Default.aspx"&gt;Jim Wooley&lt;/a&gt;, who graciously offered his help to get translate these samples and make them available to community. The newly added samples include&lt;/p&gt;  &lt;p&gt;· &lt;b&gt;&lt;a href="http://code.msdn.microsoft.com/EFExtensions"&gt;EF Extensions &lt;/a&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;· &lt;b&gt;&lt;a href="http://code.msdn.microsoft.com/EFLazyLoading"&gt;Entity Framework Lazy Loading &lt;/a&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;· &lt;b&gt;&lt;a href="http://code.msdn.microsoft.com/EFPocoAdapter"&gt;Persistence Ignorance (POCO) Adapter for Entity Framework V1 &lt;/a&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;· &lt;b&gt;&lt;a href="http://code.msdn.microsoft.com/EFDocSamples"&gt;Entity Framework Documentation Samples&lt;/a&gt; (3 new samples)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;· &lt;b&gt;&lt;a href="http://code.msdn.microsoft.com/EFQuerySamples"&gt;Entity Framework Query Samples&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Check out our newly added VB Samples for the Entity Framework at &lt;a href="http://code.msdn.microsoft.com/adonetefx"&gt;http://code.msdn.microsoft.com/adonetefx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Elisa Flasko   &lt;br /&gt;Program Manager, Data Programmability&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9654255" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author></entry><entry><title>POCO in the Entity Framework : Part 2 – Complex Types, Deferred Loading and Explicit Loading</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/05/28/poco-in-the-entity-framework-part-2-complex-types-deferred-loading-and-explicit-loading.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="16340" href="http://blogs.msdn.com/adonet/attachment/9647609.ashx" /><id>http://blogs.msdn.com/adonet/archive/2009/05/28/poco-in-the-entity-framework-part-2-complex-types-deferred-loading-and-explicit-loading.aspx</id><published>2009-05-28T16:03:00Z</published><updated>2009-05-28T16:03:00Z</updated><content type="html">&lt;P&gt;In my post last week on the &lt;A href="http://blogs.msdn.com/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx"&gt;POCO Experience in Entity Framework&lt;/A&gt;, I covered the fundamentals of POCO support in Entity Framework 4.0. In this post, I’ll cover a few more aspects related to POCO.&lt;/P&gt;
&lt;H4&gt;Complex Types&lt;/H4&gt;
&lt;P&gt;Complex Types are supported in POCO just like they are with regular &lt;B&gt;EntityObject&lt;/B&gt; based entities. All you have to do is declare your complex types as POCO classes and then use them for declaring the complex type based properties on your POCO entities.&lt;/P&gt;
&lt;P&gt;As an example, here’s an &lt;B&gt;InventoryDetail&lt;/B&gt; complex type to represent a part of my Product entity:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; &lt;FONT color=#55aaaa&gt;InventoryDetail&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#58abab&gt;Int16&lt;/FONT&gt; UnitsInStock { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#55aaaa&gt;Int16&lt;/FONT&gt; UnitsOnOrder { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#53a6a6&gt;Int16&lt;/FONT&gt; ReorderLevel { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;} &lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;My &lt;B&gt;Product&lt;/B&gt; class now has been modified to include a property of this type to group the inventory detail fields:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; &lt;FONT color=#50a0a0&gt;Product&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; ProductID { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; ProductName { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; SupplierID { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; QuantityPerUnit { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;decimal&lt;/SPAN&gt; UnitPrice { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#53a6a6&gt;InventoryDetail&lt;/FONT&gt; InventoryDetail { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;bool&lt;/SPAN&gt; Discontinued { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;FONT color=#55aaaa&gt;Category&lt;/FONT&gt; Category { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;You can then do everything you are used to doing with Complex Types. Here’s how I use it in a query:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; outOfStockProducts = &lt;FONT color=#0000ff&gt;from&lt;/FONT&gt; c &lt;SPAN style="COLOR: #0000ff"&gt;in&lt;/SPAN&gt; context.Products&lt;BR&gt;                         &lt;SPAN style="COLOR: #0000ff"&gt;where&lt;/SPAN&gt; c.InventoryDetail.UnitsInStock == 0&lt;BR&gt;                         &lt;FONT color=#0000ff&gt;select&lt;/FONT&gt; c;&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, Complex Type support with POCO is really straightforward to use. There are a couple of things you need to keep in mind when using complex types support with POCO:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You must define your Complex Type as a class. Structs are not supported.&lt;/LI&gt;
&lt;LI&gt;You cannot use inheritance with your complex type classes.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;While we are on the topic of Complex Types, I thought I’d mention one other thing: Did you know that the Entity Framework designer in Visual Studio 2010 supports complex type declarations? &lt;/P&gt;
&lt;P&gt;In Visual Studio 2008, you had to manually add the Complex Type declaration to the CSDL. That is all history with the Complex Type support in the designer in Visual Studio 2010.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_2.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_thumb.png" width=491 height=361 mce_src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And what’s cool is that because Visual Studio 2010 supports Multi-Targeting, you can use this capability when building applications that target .NET Framework 3.5, using Entity Framework 3.5 as well!&lt;/P&gt;
&lt;H4&gt;Deferred/Lazy Loading&lt;/H4&gt;
&lt;P&gt;In my &lt;A href="http://blogs.msdn.com/adonet/archive/2009/05/12/sneak-preview-deferred-loading-in-entity-framework-4-0.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/05/12/sneak-preview-deferred-loading-in-entity-framework-4-0.aspx"&gt;sneak preview post on Deferred Loading&lt;/A&gt; two weeks ago, I mentioned that there is now Deferred Loading support in Entity Framework. It comes as no surprise then that the default code-generated entity types out of the box based on &lt;B&gt;EntityObject&lt;/B&gt; will offer Deferred Loading. If you are wondering whether Deferred Loading is supported with POCO objects, then I think you will be happy to know that you can get Deferred Loading with POCO as well.&lt;/P&gt;
&lt;P&gt;There are two things you need to do in order to get Deferred Loading support with POCO entities:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Declare the property that you would like to load lazily as &lt;B&gt;virtual. &lt;/B&gt;These properties can be any collection type that implements &lt;B&gt;ICollection&amp;lt;T&amp;gt; &lt;/B&gt;or they can be a reference representing a 1/0..1 relationship.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;For instance, here’s a part of the updated &lt;B&gt;Category&lt;/B&gt; entity class which I have modified to support Deferred Loading:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; &lt;FONT color=#4b9696&gt;Category&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; CategoryID { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; CategoryName { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; Description { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;byte&lt;/SPAN&gt;[] Picture { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;FONT color=#4e9c9c&gt;List&lt;/FONT&gt;&amp;lt;&lt;FONT color=#4a9595&gt;Product&lt;/FONT&gt;&amp;gt; Products { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    ...&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2.&amp;nbsp;&amp;nbsp; Enable deferred loading on the context: &lt;BR&gt;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;context.ContextOptions.DeferredLoadingEnabled = &lt;SPAN style="COLOR: #0000ff"&gt;true&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That’s it. You will now get automatic Deferred Loading for your POCO types without having to do anything else.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;So how exactly does this work and what’s going on under the covers? &lt;/B&gt;&lt;/P&gt;
&lt;P&gt;The reason why this works is because when I marked my collection property as &lt;B&gt;virtual&lt;/B&gt;, this allowed the Entity Framework to provide a &lt;B&gt;proxy&lt;/B&gt; instance for my POCO type at runtime, and it is this proxy that does automatic deferred loading. The proxy instance is based on a type that derives from my own POCO entity class - so all functionality you have provided is preserved. From a developer point of view, this allows you to write persistence ignorant code even when deferred loading might be a requirement.&lt;/P&gt;
&lt;P&gt;If you were to inspect the actual instance in the debugger, you will see that the underlying type for the instance is different from the original type that I declared:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_4.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_thumb_1.png" width=492 height=179 mce_src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart2ComplexType_148A3/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;While the Entity Framework does its best to provide automatic deferred loading with minimal friction, this is something you need to be aware of when dealing with manual creation of instances that you want to then add or attach, or when you serialize/deserialize instances.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;Manual instantiation of Proxy instances for POCO entities&lt;/U&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;In order to enable creation of proxy instances for adding/attaching, you can use the CreateObject factory method on ObjectContext for creating entity instances:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;FONT color=#0000ff&gt;Category&lt;/FONT&gt; category = context.CreateObject&amp;lt;&lt;FONT color=#53a6a6&gt;Category&lt;/FONT&gt;&amp;gt;();&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try to keep this in mind and use &lt;B&gt;CreateObject&lt;/B&gt; when creating instances that you want to then use with the Entity Framework.&lt;/P&gt;
&lt;H4&gt;More Efficient Change Tracking with “Change Tracking Proxies”&lt;/H4&gt;
&lt;P&gt;The standard POCO entities we have talked about until now rely on snapshot based change tracking – i.e. the Entity Framework will maintain snapshots of before values and relationships of the entities so that they can be compared with current values later during Save. However, this comparison is relatively expensive when compared to the way change tracking works with EntityObject based entities. &lt;/P&gt;
&lt;P&gt;There is another type of proxy that will allow you to get better performance out of change tracking with POCO entities.&lt;/P&gt;
&lt;P&gt;If you are familiar with IPOCO, you know that &lt;B&gt;IEntityWithChangeTracker &lt;/B&gt;was one of the interfaces you were required to implement to provide change notifications to the Entity Framework.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Change Tracking proxies &lt;/B&gt;subclass your POCO entity class to provide you with this capability during runtime without requiring you to implement the IPOCO interfaces yourself. &lt;/P&gt;
&lt;P&gt;In many ways, you get the best of both worlds with this approach: You get persistence ignorance with POCO classes and you get the performance of &lt;B&gt;EntityObject&lt;/B&gt; / &lt;B&gt;IPOCO&lt;/B&gt; when it comes to change tracking. &lt;/P&gt;
&lt;P&gt;To get change tracking proxies, the basic rule is that your class must be public, non-abstract or non-sealed. Your class must also implement public virtual getters/setters for all properties that are persisted. Finally, you must declare collection based relationship navigation properties as ICollection&amp;lt;T&amp;gt; only. They cannot be a concrete implementation or another interface that derives from ICollection&amp;lt;T&amp;gt; (a difference from the Deferred Loading proxy)&lt;/P&gt;
&lt;P&gt;Here’s an example of my POCO class for &lt;B&gt;Product&lt;/B&gt; that will give me more efficient proxy based change tracking at runtime:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; &lt;FONT color=#4e9c9c&gt;Product&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; ProductID { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; ProductName { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;int&lt;/SPAN&gt; SupplierID { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt; QuantityPerUnit { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;decimal&lt;/SPAN&gt; UnitPrice { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;FONT color=#50a0a0&gt;InventoryDetail&lt;/FONT&gt; InventoryDetail { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;bool&lt;/SPAN&gt; Discontinued { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;virtual&lt;/SPAN&gt; &lt;FONT color=#54a7a7&gt;Category &lt;/FONT&gt;Category { &lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;; &lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;; }&lt;BR&gt;}&lt;BR&gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;Once again, keep in mind that you must use &lt;B&gt;CreateObject &lt;/B&gt;for creating proxy instances you want to then add or attach to the context. But pure POCO entities that don’t rely on proxies and proxy based entities can work together. You need to only use CreateObject when dealing with proxy based entities.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;What if I want both Deferred Loading and better change tracking for the same POCO type?&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;The two are not mutually exclusive and you don’t have to choose between a Deferred Loading proxy and a Change Tracking proxy. If you want Deferred Loading and efficient change tracking, you just have to follow the rules for Change Tracking via proxies, and enable Deferred Loading. Change Tracking proxies will give you deferred loading if deferred loading is enabled.&lt;/P&gt;
&lt;H4&gt;Explicit Loading&lt;/H4&gt;
&lt;P&gt;All this deferred loading capability is great – but there are plenty of you out there that want to be in full control of how you load related entities. You might even choose to go the route of pure POCO without having to resort to any of the automatic proxy generation done for you.&lt;/P&gt;
&lt;P&gt;This is a perfectly acceptable (and preferable in many cases) and you can use explicit loading of relationships and be in complete control of how you query for data from the database.&lt;/P&gt;
&lt;P&gt;There are two options for doing &lt;B&gt;Explicit Load&lt;/B&gt; with POCO:&lt;/P&gt;
&lt;P&gt;One is to use &lt;B&gt;ObjectContext.LoadProperty &lt;/B&gt;and specify the name of the navigation property you want to load:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;context.LoadProperty(beveragesCategory, &lt;SPAN style="COLOR: #006080"&gt;&lt;FONT color=#800000&gt;"Products"&lt;/FONT&gt;&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;This gets the job done – but it isn’t very type safe as you can see. If I don’t have the right name of the navigation property here, I will get a runtime exception.&lt;/P&gt;
&lt;P&gt;Some of you might actually prefer this:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4px" id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;context.LoadProperty(beveragesCategory, c =&amp;gt; c.Products);&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;I can use a lambda expression to specify the property that I want to load explicitly, and this is offers more type safety.&lt;/P&gt;
&lt;P&gt;So that’s about everything I planned on covering in the second post. There’s more to come however – in the final part of this series, we’ll go over some of the things to be aware of when dealing with pure POCO (non proxy) instances and keeping things in sync between your object graph and the Object State Manager. We will also cover variations of SaveChanges that you might want to be aware of.&lt;/P&gt;
&lt;P&gt;In the meantime, check out the sample code that I have updated to cover some of the things we have talked about in this post.&lt;/P&gt;
&lt;P&gt;Faisal Mohamood &lt;BR&gt;Program Manager, Entity Framework&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9647609" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="ADO.NET" scheme="http://blogs.msdn.com/adonet/archive/tags/ADO.NET/default.aspx" /><category term="Entity Framework" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>DataSet and Silverlight</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/05/26/dataset-and-silverlight.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/05/26/dataset-and-silverlight.aspx</id><published>2009-05-27T04:41:00Z</published><updated>2009-05-27T04:41:00Z</updated><content type="html">&lt;P&gt;I’ve been asked a few times lately whether DataSet is or will be supported in Silverlight, so I thought I’d post the answer here. &lt;/P&gt;
&lt;P&gt;There are no plans to add DataSet into future releases of Silverlight. However we are investigating work to bridge existing DataSet investments to new data technologies. Below are three different approaches that can be used to build data-centric applications in Silverlight now.&lt;/P&gt;
&lt;P&gt;ADO.NET Data Services is the primary approach to creating data-centric applications in Silverlight. There is a client api within Silverlight for accessing these RESTful Services. ADO.NET Data Services exposes data through ATOM and JSON which are standard technologies when building rich internet applications. Here is a link to get started on this technology: &lt;A href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" mce_href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;http://msdn.microsoft.com/en-us/data/bb931106.aspx&lt;/A&gt;.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;In addition to ADO.NET Data Services, there is .Net RIA Services. The project aims to allow developers to build rich internet applications on Silverlight using Visual Studio. This isn't a released project yet, but there May 2009 preview that developers can use. Here is a link to this preview: &lt;A href="http://silverlight.net/forums/53.aspx" mce_href="http://silverlight.net/forums/53.aspx"&gt;http://silverlight.net/forums/53.aspx&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;Lastly, if there are requirements to expose data through Web Services rather than RESTful Services, we would recommend using WCF. These services can be accessed within Silverlight and would expose specific data contracts, not DataSets. Here is a link to get started on this approach: &lt;A href="http://msdn.microsoft.com/en-us/magazine/cc794260.aspx" mce_href="http://msdn.microsoft.com/en-us/magazine/cc794260.aspx"&gt;http://msdn.microsoft.com/en-us/magazine/cc794260.aspx&lt;/A&gt;. We welcome any feedback from the community on this.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;BR&gt;Chris Robinson&lt;BR&gt;Program Manager - DataSet&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9643460" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="ADO.NET" scheme="http://blogs.msdn.com/adonet/archive/tags/ADO.NET/default.aspx" /><category term="DataSet" scheme="http://blogs.msdn.com/adonet/archive/tags/DataSet/default.aspx" /></entry><entry><title>POCO in the Entity Framework: Part 1 - The Experience</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx" /><link rel="enclosure" type="application/x-zip-compressed" length="20480" href="http://blogs.msdn.com/adonet/attachment/9634552.ashx" /><id>http://blogs.msdn.com/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx</id><published>2009-05-22T00:46:00Z</published><updated>2009-05-22T00:46:00Z</updated><content type="html">&lt;P&gt;Last week I mentioned in the &lt;A href="http://blogs.msdn.com/adonet/archive/2009/05/11/sneak-preview-persistence-ignorance-and-poco-in-entity-framework-4-0.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/05/11/sneak-preview-persistence-ignorance-and-poco-in-entity-framework-4-0.aspx"&gt;sneak preview on POCO&lt;/A&gt; that support for POCO entities is one of the new capabilities we have added to Entity Framework 4.0. This week, I’d like to go into the details of POCO support in Entity Framework 4.0.&lt;/P&gt;
&lt;P&gt;There’s quite a bit to discuss here, including:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Overall POCO experience in Entity Framework 4.0&lt;/LI&gt;
&lt;LI&gt;Change Tracking in POCO&lt;/LI&gt;
&lt;LI&gt;Relationship Fix-up&lt;/LI&gt;
&lt;LI&gt;Complex Types&lt;/LI&gt;
&lt;LI&gt;Deferred (Lazy) Loading and Explicit Loading&lt;/LI&gt;
&lt;LI&gt;Best Practices&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;In this post, I will focus primarily on the overall experience so that you can get started with POCO in Entity Framework 4.0 right away. I’d like to use a simple example that we can walk through so you can see what it feels like to use POCO in Entity Framework 4.0. I will use the Northwind database, and&amp;nbsp; we’ll continue to build on this example in subsequent posts.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Step 1 – Create the Model, turn off default Code Generation&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;While POCO allows you to write your own entity classes in a persistence ignorant fashion, there is still the need for you to “plug in” persistence and EF metadata so that your POCO entities can be materialized from the database and persisted back to the database. In order to do this, you will still need to either create an Entity Data Model using the Entity Framework Designer or provide the CSDL, SSDL and MSL metadata files exactly as you have done with Entity Framework 3.5. So first I’ll generate an EDMX using the ADO.NET Entity Data Model Wizard.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_2.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_thumb.png" width=491 height=341 mce_src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a class library project for defining your POCO types. I named mine &lt;B&gt;NorthwindModel&lt;/B&gt;. This project will be persistence ignorant and will not have a dependency on the Entity Framework.&lt;/LI&gt;
&lt;LI&gt;Create a class library project that will contain your persistence aware code. I named mine &lt;B&gt;NorthwindData. &lt;/B&gt;This project will have a dependency on Entity Framework (System.Data.Entity) in addition to a dependency on the &lt;B&gt;NorthwindModel&lt;/B&gt; project.&lt;/LI&gt;
&lt;LI&gt;&lt;B&gt;Add New Item &lt;/B&gt;to the &lt;B&gt;NorthwindData&lt;/B&gt; project and add an ADO.NET Entity Data Model called &lt;B&gt;Northwind.edmx &lt;/B&gt;(doing this will automatically add the dependency to the Entity Framework).&lt;/LI&gt;
&lt;LI&gt;Go through “Generate from Database” and build a model for the Northwind database. &lt;/LI&gt;
&lt;LI&gt;For now, select &lt;B&gt;Categories&lt;/B&gt; and &lt;B&gt;Products&lt;/B&gt; as the only two tables you are interested in adding to your Entity Data model.&lt;B&gt;&lt;/B&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Now that I have my Entity Data model to work with, there is one final step before I start to write code : turn off code generation. After all you are interested in POCO – so remove the &lt;B&gt;Custom Tool &lt;/B&gt;that is responsible for generating &lt;B&gt;EntityObject&lt;/B&gt; based code for &lt;B&gt;Northwind.edmx. &lt;/B&gt;This will turn off code generation for your model.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_4.png"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=image border=0 alt=image src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_thumb_1.png" width=501 height=348 mce_src="http://blogs.msdn.com/blogfiles/adonet/WindowsLiveWriter/POCOintheEntityFrameworkPart1TheExperien_F9E4/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;We are now ready to write our POCO entities.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Step 2 – Code up your POCO entities&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;I am going to write simple POCO entities for Category and Product. These will be added to the &lt;B&gt;NorthwindModel&lt;/B&gt; project. Note that what I show here shouldn’t be taken as best practice and the intention here is to demonstrate the simplest case that works out of the box. We will extend and customize this to our needs as we go forward and build on top of this using &lt;B&gt;Repository&lt;/B&gt; and &lt;B&gt;Unit of Work&lt;/B&gt; patterns later on.&lt;/P&gt;
&lt;P&gt;Here’s sample code for our Category entity:&lt;/P&gt;&lt;PRE class=csharpcode&gt;    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; &lt;FONT color=#54a7a7&gt;Category&lt;/FONT&gt;
    {
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; CategoryID { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; CategoryName { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; Description { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;byte&lt;/SPAN&gt;[] Picture { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;FONT color=#5fafaf&gt;List&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;Product&lt;/FONT&gt;&amp;gt; Products { get; set; }
    }&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Note that I have defined properties for scalar properties as well as navigation properties in my model. The Navigation Property in our model translates to a List&amp;lt;Product&amp;gt;.&lt;/P&gt;
&lt;P&gt;Similarly, Product entity can be coded like this:&lt;/P&gt;&lt;PRE class=csharpcode&gt;    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; &lt;FONT color=#54a9a9&gt;Product&lt;/FONT&gt;
    {
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; ProductID { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; ProductName { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; SupplierID { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; QuantityPerUnit { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;decimal&lt;/SPAN&gt; UnitPrice { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;FONT color=#54a7a7&gt;Int16&lt;/FONT&gt; UnitsInStock { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;FONT color=#58abab&gt;Int16 &lt;/FONT&gt;UnitsOnOrder { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;FONT color=#54a9a9&gt;Int16&lt;/FONT&gt; ReorderLevel { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;bool&lt;/SPAN&gt; Discontinued { get; set; }
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;FONT color=#5fafaf&gt;Category&lt;/FONT&gt; Category { get; set; }
    }&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;In this case, since the relationship allows only one &lt;B&gt;Category&lt;/B&gt; that a &lt;B&gt;Product&lt;/B&gt; can belong to, we have a reference to a &lt;B&gt;Category&lt;/B&gt; (unlike the &lt;B&gt;List&amp;lt;T&amp;gt;&lt;/B&gt; collection we have for modeling the Navigation Property in &lt;B&gt;Category&lt;/B&gt;).&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Step 3 – Code up your Entity Framework Context&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;The last thing I have to do in order to pull all of this together is to provide a context implementation (much like the &lt;B&gt;ObjectContext&lt;/B&gt; implementation you get when you use default code generation). The context is the glue that brings persistence awareness into your application, and it will allow you to compose queries, materialize entities as well as save changes back to the database. The context will be a part of the &lt;B&gt;NorthwindData&lt;/B&gt; project.&lt;/P&gt;
&lt;P&gt;For simplicity, I will include our context into the same class library that has our entity types – but when we discuss patterns such as &lt;B&gt;Repository &lt;/B&gt;and &lt;B&gt;Unit of Work&lt;/B&gt;, we will set it up such that you have a pure POCO class library without any persistence concerns.&lt;/P&gt;
&lt;P&gt;Here’s a simple context implementation for our scenario:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; &lt;FONT color=#5fafaf&gt;NorthwindContext &lt;FONT color=#000000&gt;:&lt;/FONT&gt; ObjectContext&lt;/FONT&gt;
    {   
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; NorthwindContext() : &lt;SPAN class=kwrd&gt;base&lt;/SPAN&gt;&lt;FONT color=#800000&gt;&lt;FONT color=#000000&gt;(&lt;/FONT&gt;"name=NorthwindEntities"&lt;/FONT&gt;&lt;FONT color=#000000&gt;,&lt;/FONT&gt; &lt;BR&gt;&lt;FONT color=#800000&gt;"NorthwindEntities"&lt;/FONT&gt;)  
        {
            _categories = CreateObjectSet&amp;lt;&lt;FONT color=#58abab&gt;Category&lt;/FONT&gt;&amp;gt;();
            _products = CreateObjectSet&amp;lt;&lt;FONT color=#50a0a0&gt;Product&lt;/FONT&gt;&amp;gt;();
        }

        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;FONT color=#59acac&gt;ObjectSet&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;Category&lt;/FONT&gt;&amp;gt; Categories
        {
            get 
            { 
                &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; _categories; 
            }
        }
        &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;FONT color=#59acac&gt;ObjectSet&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;Category&lt;/FONT&gt;&amp;gt; _categories;

        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;FONT color=#59acac&gt;ObjectSet&lt;/FONT&gt;&amp;lt;&lt;FONT color=#50a0a0&gt;Product&lt;/FONT&gt;&amp;gt; Products
        {
            get
            {
                &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; _products;
            }
        }
        &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;FONT color=#59acac&gt;ObjectSet&lt;/FONT&gt;&amp;lt;&lt;FONT color=#50a0a0&gt;Product&lt;/FONT&gt;&amp;gt; _products;
    }&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;Note that &lt;B&gt;ObjectSet&lt;/B&gt;&amp;lt;&lt;B&gt;T&lt;/B&gt;&amp;gt; is a more specialized &lt;B&gt;ObjectQuery&lt;/B&gt;&amp;lt;&lt;B&gt;T&lt;/B&gt;&amp;gt; that we introduced in Entity Framework 4.0.&lt;/P&gt;
&lt;P&gt;That’s it – now you have pure POCO entities with a simple context that will allow you to write queries like this:&lt;/P&gt;&lt;PRE class=csharpcode&gt;   &lt;FONT color=#52a3a3&gt; NorthwindContext&lt;/FONT&gt; db = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;FONT color=#4e9a9a&gt;NorthwindContext&lt;/FONT&gt;();

    &lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; beverages = &lt;FONT color=#0000ff&gt;from&lt;/FONT&gt; p &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; db.Products
                    &lt;SPAN class=kwrd&gt;where&lt;/SPAN&gt; p.Category.CategoryName == &lt;SPAN class=str&gt;&lt;FONT color=#800000&gt;"Beverages"&lt;/FONT&gt;&lt;/SPAN&gt;
                    &lt;FONT color=#0000ff&gt;select&lt;/FONT&gt; p;&lt;/PRE&gt;
&lt;P&gt;The entities that are materialized are pure POCO entities, and you can make and persist changes much like you would with regular &lt;B&gt;EntityObject&lt;/B&gt; or &lt;B&gt;IPOCO&lt;/B&gt; entities. You get all the services provided by Entity Framework - the only difference is that you are using pure POCO entities.&lt;/P&gt;
&lt;P&gt;There are many possibilities here as far as how we can improve on this simplified example. Before we get into that however, I would like to get some basic questions out of the way.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Do I need an Entity Data Model before I can use POCO?&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;Yes – POCO support in Entity Framework 4.0 simply removes the need of having persistence specific concerns in your entity classes. There is still the need for you to have a CSDL/SSDL/MSL (collectively EDMX) metadata so that the Entity Framework is able to use your entities along with the metadata in order to enable data access. There is a separate effort that we are working on that will allow you to do true “code-first” development without the need to have a predefined EDMX model. A community preview of this feature will be released to the web in the coming months. We will roll this into the product the first chance we get. As always, your feedback will be helpful.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Do I have to always hand-craft these entities and the context?&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;No – there is a very powerful and flexible code generation mechanism in Entity Framework 4.0 that is based on T4 as Alex blogged about &lt;A href="http://blogs.msdn.com/adonet/archive/2009/05/19/sneak-peek-using-code-generation-templates-with-the-entity-framework-4-0.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/05/19/sneak-peek-using-code-generation-templates-with-the-entity-framework-4-0.aspx"&gt;here&lt;/A&gt;. You can provide your own templates that allow you to build your entities in a way that you see fit. We are also working on providing standard out of the box templates that will generate POCO entities for you.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;How is metadata mapped when using POCO entities?&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;In Entity Framework 3.5, both &lt;B&gt;EntityObject&lt;/B&gt; and &lt;B&gt;IPOCO&lt;/B&gt; based entities relied on the use of mapping attributes that were meant for decorating and mapping the entity types and properties back to the corresponding elements in the Conceptual model. Entity Framework 4.0 introduces convention based mapping for allowing mapping of Entity Types, Properties, Complex Types and Relationships back to the conceptual model without the need for explicit decoration. The simple rule here is that Entity Type names, Property names and Complex Types names used in your POCO classes must match those defined by the conceptual model. Namespace names are ignored and don’t have to match between the class definition and the conceptual model.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Do I need to have public getters and setters for all properties in my entity classes?&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;You can use any access modifier on your POCO type’s properties as long as none of the mapped properties are virtual and as long as you don’t require partial trust support. When running in partial trust, there are specific requirements on the visibility of access modifiers on your entity classes. We will be documenting the complete set of access modifiers that are supported when partial trust is involved.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;What types of collections are supported for collection based navigation properties?&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;Any collection that is an &lt;B&gt;ICollection&amp;lt;T&amp;gt;&lt;/B&gt; will be supported. If you don’t initialize the field with a concrete type that is an &lt;B&gt;ICollection&amp;lt;T&amp;gt;&lt;/B&gt; type, then an &lt;B&gt;List&amp;lt;T&amp;gt;&lt;/B&gt; type will be provided upon materialization.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Can I have uni-directional relationships? For instance – I would like to have a Category property in my Product class, but I don’t want to have a Products collection in my Category class.&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;Yes – this is supported. The only restriction here is that your entity types have to reflect what is defined by the model. If you are not interested in having the navigation property for one side of the relationship, then remove it from the model completely.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;Is Deferred (Lazy) Loading supported with POCO?&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;Yes – Deferred (Lazy) loading is supported with POCO through the use of proxy types that are used to provide automatic lazy loading behavior on top of your POCO classes. This is something that we’ll cover when we get to deferred loading – until then know that eager loading via the use of “&lt;B&gt;Include&lt;/B&gt;” is also supported, like so:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;FONT color=#0000ff&gt;var&lt;/FONT&gt; beverageCategory = (&lt;FONT color=#0000ff&gt;from&lt;/FONT&gt; c &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; context.Categories&lt;BR&gt;.Include(&lt;SPAN class=str&gt;&lt;FONT color=#800000&gt;"Products"&lt;/FONT&gt;&lt;/SPAN&gt;)
                        &lt;SPAN class=kwrd&gt;where&lt;/SPAN&gt; c.CategoryName == &lt;SPAN class=str&gt;&lt;FONT color=#800000&gt;"Beverages"&lt;/FONT&gt;&lt;/SPAN&gt;
                        &lt;FONT color=#0000ff&gt;select&lt;/FONT&gt; c).Single();&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;If I were to use Deferred (Lazy) Loading, I don’t have to do this – we’ll discuss that when we discuss proxies.&lt;/P&gt;
&lt;H4&gt;Fixing up Relationships&lt;/H4&gt;
&lt;P&gt;There are two types of fix-ups to be aware of: 1)Fix-up during query, and 2) Fix-up during changes to your entities/relationships&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;Fix-up during Query&lt;/U&gt;&lt;/B&gt;&lt;U&gt; &lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Fix-up during query happens when you load related entities using separate queries on the same &lt;B&gt;ObjectContext&lt;/B&gt;. For instance, if I were to query for a category instance &lt;B&gt;Beverages&lt;/B&gt;, and later query for a product instance &lt;B&gt;Chai&lt;/B&gt; (that is in Category &lt;B&gt;Beverages&lt;/B&gt;), I would want &lt;B&gt;chai.Category&lt;/B&gt; to point to the instance of the &lt;B&gt;Beverages&lt;/B&gt; category without additional work.&lt;/P&gt;
&lt;P&gt;This is automatic and works today.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;Fix-up during changes to your entities/relationships&lt;/U&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;This is the relationship fix-up between two entities when I add/remove an entity that is related to another entity or alter relationships. Think of this as the case when I create a new product called &lt;B&gt;“Diet Chai”&lt;/B&gt; and want to associate it with the &lt;B&gt;Beverages &lt;/B&gt;category&lt;B&gt;.&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;In Beta1 of Entity Framework 4.0, you do not get automatic relationship fix-up in this case with POCO entities. When dealing with relationships that change between entities, you will have to make sure that your POCO types include the logic to manage fix-up on both ends of the relationship correctly. &lt;/P&gt;
&lt;P&gt;For instance, in my Northwind example, I have defined the following method on &lt;B&gt;Category&lt;/B&gt; to support adding / removing of Orders.&lt;/P&gt;&lt;PRE class=csharpcode&gt;        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; AddProduct(&lt;FONT color=#54a7a7&gt;Product&lt;/FONT&gt; p)
        {
            &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (Products == &lt;SPAN class=kwrd&gt;null&lt;/SPAN&gt;)
            {
                Products = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; &lt;FONT color=#4e9a9a&gt;List&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;Product&lt;/FONT&gt;&amp;gt;();
            }

            &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (!Products.Contains(p))
            {
                Products.Add(p);
            }            
            
            p.Category = &lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;;
        }&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;In general, it is good to use a pattern like this to support adding/removing related items rather than using the relationship collection to add/remove related entities. You also have options of making the getter/setter for the actual EF backed collection private/internal to support more fine grained access – but as mentioned earlier, some of this depends on the requirements as to whether you require partial trust support or not.&lt;/P&gt;
&lt;P&gt;So we have covered quite a bit of ground in this “quick look” at the overall POCO experience. There’s quite a lot more to talk about – and I will be continuing this discussion early next week. In the meantime, take a look at the complete solution (attached) if you are interested in the sample code covering all the concepts we covered here. Please keep in mind that you must have a local instance of Northwind database installed on the machine where you are running this sample.&lt;/P&gt;
&lt;P&gt;In my next post we will look at Complex Types, Change Tracking, Proxies, Lazy Loading and Eager Loading. Until then, please look through the MSDN Documentation on POCO &lt;A href="http://msdn.microsoft.com/en-us/library/dd456853(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd456853(VS.100).aspx"&gt;here&lt;/A&gt; for more details on POCO support in Entity Framework 4.0.&lt;/P&gt;
&lt;P&gt;Let us know what you think!&lt;/P&gt;
&lt;P&gt;Faisal Mohamood &lt;BR&gt;Program Manager, Entity Framework&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9634552" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author><category term="Entity Framework" scheme="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>Updated Entity Framework Documentation for Beta1</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/05/21/updated-entity-framework-documentation-for-beta1.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/05/21/updated-entity-framework-documentation-for-beta1.aspx</id><published>2009-05-21T15:38:48Z</published><updated>2009-05-21T15:38:48Z</updated><content type="html">&lt;p&gt;With the release of &lt;a href="http://go.microsoft.com/fwlink/?LinkId=152517"&gt;Visual Studio 2010 Beta 1&lt;/a&gt;, we have released supporting &lt;a href="http://msdn.microsoft.com/en-us/library/bb399572(VS.100).aspx"&gt;Entity Framework documentation&lt;/a&gt;. Below are links to documentation for new Entity Framework features and scenarios. Along with feedback on the Entity Framework, we would like to hear your feedback on the documentation. What works for you? What doesn’t? What’s missing? We’ll work to incorporate your feedback in future releases.&lt;/p&gt;  &lt;p&gt;Thanks!&lt;/p&gt;  &lt;p&gt;&lt;i&gt;Entity Framework User Education Team&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Persistence-Ignorant Objects&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;You can use your own custom data classes together with your data model without making any modifications to the data classes themselves. This means that you can use &amp;quot;plain old&amp;quot; CLR objects (POCO), such as existing domain objects, with your Entity Framework application. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/dd456853(VS.100).aspx"&gt;Persistence Ignorant Objects (Entity Framework)&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Deferred Loading of Related Objects&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;With deferred loading, also known as &lt;i&gt;lazy loading&lt;/i&gt;, related objects are automatically loaded from the data source when you access a navigation property. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/bb896272(VS.100).aspx"&gt;Shaping Query Results (Entity Framework)&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Functions in LINQ to Entities Queries&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions(VS.100).aspx"&gt;EntityFunctions&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions(VS.100).aspx"&gt;SqlFunctions&lt;/a&gt; classes provide access to canonical and database functions from LINQ to Entities queries. The &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.edmfunctionattribute(VS.100).aspx"&gt;EdmFunctionAttribute&lt;/a&gt; allows a CLR method to serve as a proxy for a function defined in the conceptual model or storage model. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/dd456828(VS.100).aspx"&gt;Calling Functions in LINQ to Entities Queries&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Customized Object Layer Code Generation&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;You can configure the &lt;a href="http://msdn.microsoft.com/en-us/library/cc716685(VS.100).aspx"&gt;ADO.NET Entity Data Model Designer&lt;/a&gt; to use text templates to generate customized object layer code. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/dd456821(VS.100).aspx"&gt;How to: Customize Object Layer Code Generation&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Model-First Support&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/dd456817(VS.100).aspx"&gt;Create Database Wizard&lt;/a&gt; enables you to do conceptual modeling first, and then create a database that supports the model. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/dd456815(VS.100).aspx"&gt;How to: Generate a Database from a Conceptual Model&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Complex Type Support in the Entity Data Model Designer&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/cc716685(VS.100).aspx"&gt;ADO.NET Entity Data Model Designer&lt;/a&gt; now supports complex types. For more information, see the following topics:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd456820(VS.100).aspx"&gt;How to: Create and Modify Complex Types&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd456823(VS.100).aspx"&gt;How to: Add a Complex Type to an Entity Type&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd456824(VS.100).aspx"&gt;How to: Map a Function Import to a Complex Type&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd456822(VS.100).aspx"&gt;How to: Map Complex Type Properties to Table Columns&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Naming Service&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/bb399247(VS.100).aspx"&gt;Entity Data Model Wizard&lt;/a&gt; and the &lt;a href="http://msdn.microsoft.com/en-us/library/cc716705(VS.100).aspx"&gt;Update Model Wizard&lt;/a&gt; provide the option of using singular or plural forms of &lt;b&gt;Entity&lt;/b&gt;, &lt;b&gt;EntitySet&lt;/b&gt;, and &lt;b&gt;NavigationProperty&lt;/b&gt; names to make application code more readable. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/bb399253(VS.100).aspx"&gt;Choose Your Database Objects Dialog Box (Entity Data Model Wizard)&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/cc716689(VS.100).aspx"&gt;Choose Your Database Objects Dialog Box (Update Model Wizard)&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Improved Model Browser Functionality&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The &lt;b&gt;Model Browser&lt;/b&gt; window of the &lt;a href="http://msdn.microsoft.com/en-us/library/cc716685(VS.100).aspx"&gt;ADO.NET Entity Data Model Designer&lt;/a&gt; enables you to delete objects from the storage model and to search the conceptual and storage models for a specified string. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/bb738483(VS.100).aspx"&gt;Model Browser Window&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/dd456816(VS.100).aspx"&gt;How to: Delete Objects from the Storage Model&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;User-Defined Functions (Entity SQL)&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Entity SQL supports calling user-defined functions in a query. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/dd490950(VS.100).aspx"&gt;User-Defined Functions (Entity SQL)&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9633903" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author></entry><entry><title>Entity Framework 4.0 Beta 1 bits now available in the VS 2010 and .NET 4 Beta 1</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/adonet/archive/2009/05/20/entity-framework-4-0-beta-1-bits-now-available-in-the-vs-2010-and-net-4-beta-1.aspx" /><id>http://blogs.msdn.com/adonet/archive/2009/05/20/entity-framework-4-0-beta-1-bits-now-available-in-the-vs-2010-and-net-4-beta-1.aspx</id><published>2009-05-21T04:58:20Z</published><updated>2009-05-21T04:58:20Z</updated><content type="html">&lt;p&gt;Beta 1 has shipped!&amp;#160; You can find information about the Beta and pointers to the download &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;here&lt;/a&gt;.&amp;#160; So far on this bog we’ve &lt;a href="http://blogs.msdn.com/adonet/archive/2009/05/11/update-on-the-entity-framework-in-net-4-and-visual-studio-2010.aspx"&gt;talked&lt;/a&gt;, at a high level, about the scenarios and capabilities that our team has been focused on in this release.&amp;#160; In the coming weeks and months, we’ll be drilling in depth on these topics.&amp;#160; Starting tomorrow we’ll have the first post on POCO, so stay tuned.&amp;#160; Beyond reading and posting questions to the team blog, you can look to the &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads"&gt;forums&lt;/a&gt; for help and information, &lt;a href="http://msdn.microsoft.com/en-us/library/ex6y04yf(VS.100).aspx"&gt;read&lt;/a&gt; the what’s new in ADO.NET section in the MSDN documentation, and file bugs on the &lt;a href="http://connect.microsoft.com/VisualStudio/content/content.aspx?ContentID=12362"&gt;connect site&lt;/a&gt;.&amp;#160; We look forward to hearing any feedback you have on the updates we’ve made.&lt;/p&gt;  &lt;p&gt;Carl Perry&lt;/p&gt;  &lt;p&gt;Program Manager, Entity Framework&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9633326" width="1" height="1"&gt;</content><author><name>dpblogs</name><uri>http://blogs.msdn.com/members/dpblogs.aspx</uri></author></entry></feed>