<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jaroslaw Kowalski : EFPocoAdapter</title><link>http://blogs.msdn.com/jkowalski/archive/tags/EFPocoAdapter/default.aspx</link><description>Tags: EFPocoAdapter</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Entity Framework samples converted to Visual Basic </title><link>http://blogs.msdn.com/jkowalski/archive/2009/05/21/entity-framework-samples-converted-to-visual-basic.aspx</link><pubDate>Thu, 21 May 2009 20:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9634089</guid><dc:creator>jkowalski</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jkowalski/comments/9634089.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jkowalski/commentrss.aspx?PostID=9634089</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jkowalski/rsscomments.aspx?PostID=9634089</wfw:comment><description>&lt;P&gt;We have recently published Visual Basic versions of our MSDN Code Gallery samples for Entity Framework v3.5SP1.&lt;/P&gt;
&lt;P&gt;The following translated samples are currently available in two languages (C# and VB)&lt;/P&gt;
&lt;P&gt;ADO.NET Entity Framework Query Samples&lt;BR&gt;&lt;A href="http://code.msdn.com/EFQuerySamples" mce_href="http://code.msdn.com/EFQuerySamples"&gt;http://code.msdn.com/EFQuerySamples&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Persistence Ignorance (POCO) Adapter for Entity Framework&lt;BR&gt;&lt;A href="http://code.msdn.com/EFPocoAdapter" mce_href="http://code.msdn.com/EFPocoAdapter"&gt;http://code.msdn.com/EFPocoAdapter&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Transparent Lazy Loading for Entity Framework&lt;BR&gt;&lt;A href="http://code.msdn.com/EFLazyLoading" mce_href="http://code.msdn.com/EFLazyLoading"&gt;http://code.msdn.com/EFLazyLoading&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;The samples have been fully converted to use and generate Visual Basic&amp;nbsp;code instead of C#. More samples are in the pipeline and will be published soon - stay tuned. &lt;/P&gt;
&lt;P&gt;Full list of current EF samples published by the Entity Framework team is always available at:&lt;BR&gt;&lt;A href="http://code.msdn.microsoft.com/adonetefx" mce_href="http://code.msdn.microsoft.com/adonetefx"&gt;http://code.msdn.microsoft.com/adonetefx&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9634089" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jkowalski/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/jkowalski/archive/tags/EFLazyLoading/default.aspx">EFLazyLoading</category><category domain="http://blogs.msdn.com/jkowalski/archive/tags/EFPocoAdapter/default.aspx">EFPocoAdapter</category></item><item><title>EF POCO Adapter updated (v1.03)</title><link>http://blogs.msdn.com/jkowalski/archive/2008/09/25/ef-poco-adapter-updated-v1-03.aspx</link><pubDate>Fri, 26 Sep 2008 06:24:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8965874</guid><dc:creator>jkowalski</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/jkowalski/comments/8965874.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jkowalski/commentrss.aspx?PostID=8965874</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jkowalski/rsscomments.aspx?PostID=8965874</wfw:comment><description>&lt;p&gt;I've just posted another round of updates to EF POCO Adapter based on feedback from users. This set of changes is focused on N-tier scenarios and detached entities (it adds missing wrappers for EF APIs and fixes behavior of existing ones).&lt;/p&gt; &lt;p&gt;Change log:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Added wrapper for ObjectContext.ApplyPropertyChanges() to IEntitySet&amp;lt;T&amp;gt;  &lt;li&gt;Added wrapper for ObjectContext.Detach() to IEntitySet&amp;lt;T&amp;gt;  &lt;li&gt;Fixed handling of detached/out-of-scope entities w.r.t lazy loading and attaching to other contexts. Lazy loading outside of scope will do nothing if the context had DeferredLoadingEnabled=false, otherwise will throw.  &lt;li&gt;Added partial OnZZZCreated() methods on generated contexts, adapters and proxies to enable integration with 3rd-party extensions  &lt;li&gt;Fixed code generation from EDMX.  &lt;li&gt;EFPocoContext.EnableLazyLoading has been renamed to EFPocoContext.DeferredLoadingEnabled to better align with Linq to SQL and our plans for EF v2  &lt;li&gt;Fixed adapter-POCO synchronization for complex types&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The new release is here: &lt;a href="http://code.msdn.microsoft.com/EFPocoAdapter/Release/ProjectReleases.aspx?ReleaseId=1580"&gt;http://code.msdn.microsoft.com/EFPocoAdapter/Release/ProjectReleases.aspx?ReleaseId=1580&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Big thanks to everyone who sent me comments, bug reports and improvement suggestions for this release. This is very valuable feedback as it allows us to better understand user scenarios around POCOs for EF v2.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8965874" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jkowalski/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/jkowalski/archive/tags/EFPocoAdapter/default.aspx">EFPocoAdapter</category></item><item><title>EF POCO Adapter updated (v1.02)</title><link>http://blogs.msdn.com/jkowalski/archive/2008/09/13/ef-poco-adapter-updated-v1-02.aspx</link><pubDate>Sun, 14 Sep 2008 02:36:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8951316</guid><dc:creator>jkowalski</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jkowalski/comments/8951316.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jkowalski/commentrss.aspx?PostID=8951316</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jkowalski/rsscomments.aspx?PostID=8951316</wfw:comment><description>&lt;p&gt;I've just posted updates to EF POCO Adapter (v1.02) which resolves a couple of issues spotted by users:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Detached proxies (created by EFPocoContxt.CreateObject&amp;lt;T&amp;gt;) as well as proxies in the Added state were failing on property navigation. This has been fixed, and the property navigation doesn't attempt the load in one of these states. Thanks to &lt;a href="http://polymorphicview.blogspot.com/"&gt;Corey Gaudin&lt;/a&gt; for reporting this.&lt;/li&gt; &lt;li&gt;GetAdapterObject() was failing for proxies created with CreateObject&amp;lt;T&amp;gt;. This has been fixed by ensuring that adapters are always cached whenever proxies are present. As a side effect of this change, DetectChanges() should be a bit faster, as we don't have to walk object state manager anymore. Thanks to Corey Gaudin for reporting this.&lt;/li&gt; &lt;li&gt;Fixed code generator to eliminate compilation warning of unused property.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;The updated version is available &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=EFPocoAdapter&amp;amp;ReleaseId=1537"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8951316" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jkowalski/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/jkowalski/archive/tags/EFPocoAdapter/default.aspx">EFPocoAdapter</category></item><item><title>Persistence Ignorance (POCO) Adapter for Entity Framework V1</title><link>http://blogs.msdn.com/jkowalski/archive/2008/09/09/persistence-ignorance-poco-adapter-for-entity-framework-v1.aspx</link><pubDate>Tue, 09 Sep 2008 19:24:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8936938</guid><dc:creator>jkowalski</dc:creator><slash:comments>25</slash:comments><comments>http://blogs.msdn.com/jkowalski/comments/8936938.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jkowalski/commentrss.aspx?PostID=8936938</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jkowalski/rsscomments.aspx?PostID=8936938</wfw:comment><description>&lt;p&gt;One of the limitations of Entity Framework V1 released as part of .NET Framework 3.5 SP1 and Visual Studio 2008 SP1 is lack of support for POCO entity objects. POCO stands for Plain Old CLR Object and refers to an object that does not have any persistence concerns and is not aware of persistence framework. Modern development approaches such as DDD (Domain-Driven Design) and TDD (Test-Driven Development) prefer clear separation of concerns which implies persistence ignorance.&lt;/p&gt; &lt;p&gt;In order to be consumable by EF, entity classes have to:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;div&gt;Derive from EntityObject class or implement at least one mandatory IPOCO interface: IEntityWithChangeTracker. If the entity participates in relationships it also has to implement IEntityWithRelationships interface. Implementing those interfaces is not hard, but causes entity objects to have a dependency on EF assembly, which is sometimes not desirable&lt;/div&gt; &lt;li&gt; &lt;div&gt;Provide assembly, class and property-level attributes to define mapping from CLR space to model space (we call those O-C mapping attributes)&lt;/div&gt; &lt;li&gt; &lt;div&gt;Use Entity-Framework-provided relationship classes: EntityReference&amp;lt;T&amp;gt;, EntityCollection&amp;lt;T&amp;gt; and RelationshipManager instead of CLR collections (List&amp;lt;T&amp;gt;, ICollection&amp;lt;T&amp;gt;)&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Clearly those requirements make EF’s entity objects persistence-aware.&lt;/p&gt; &lt;p&gt;It is possible to write (generate) an adapter layer that will translate between POCO objects and Entity Framework-aware objects and provide services on top of POCO objects, such as:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;div&gt;Change tracking (snapshot-based and proxy-based when possible)&lt;/div&gt; &lt;li&gt; &lt;div&gt;Transparent lazy loading&lt;/div&gt; &lt;li&gt; &lt;div&gt;Immutable Value Objects&lt;/div&gt; &lt;li&gt; &lt;div&gt;Queries (LINQ and Entity SQL)&lt;/div&gt; &lt;li&gt; &lt;div&gt;Shadow state (maintaining certain persistence-related fields outside of entity class)&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;We have recently published a sample called &lt;a href="http://code.msdn.microsoft.com/EFPocoAdapter"&gt;EFPocoAdapter&lt;/a&gt; that includes code generator and a supporting library that implements POCO object tracking on top of Entity Framework V1. This serves two purposes:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;div&gt;To demonstrate that it is possible to track POCO objects using the current version of EF&lt;/div&gt; &lt;li&gt; &lt;div&gt;To gather feedback on POCO-specific features (such as transparent lazy loading, immutable objects, change detection, etc) that customers would like to see implemented natively in future versions of Entity Framework.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The source code is released under Microsoft Public License on Code Gallery. There is no formal support for this sample, but we are interested in improving and evolving it based on user’s feedback. For legal reasons we cannot accept source code contributions, but you are more than welcome to share feature ideas and POCO usage scenarios with us.&lt;/p&gt; &lt;h4&gt;Adapter Design&lt;/h4&gt; &lt;p&gt;The main idea used when implementing EFPocoAdapter was the use of adapter objects. Adapters are classes that are EF-friendly (in terms of O/C mapping, relationship manager and so on), but internally manage and materialize POCO objects. Materialization is done transparently by delegating property setters and association change notifications to the POCO object. This way, whenever Entity Framework materializes (hydrates) an adapter object, a corresponding POCO object is also created. Note that property setters not only delegate values to POCO objects, but they also store a copy for themselves – this helps later in the change detection process. &lt;/p&gt; &lt;p&gt;Let’s take a look at a simple adapter class on top of a Region POCO that has 2 properties: RegionID and RegionDescription. It has [EdmEntityType] attribute and [EdmScalarProperty] attributes on every property. Every property setter delegates property write to corresponding property on _pocoEntity object along with firing events necessary for change tracking. A copy of object state is stored in _RegionID and _RegionDescription properties.&lt;/p&gt;&lt;pre class="csharpcode"&gt;[EdmEntityType(NamespaceName=&lt;span class="str"&gt;"NorthwindEFModel"&lt;/span&gt;, Name=&lt;span class="str"&gt;"Region"&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RegionAdapter : PocoAdapterBase&amp;lt;NorthwindEF.Region&amp;gt;, IPocoAdapter&amp;lt;NorthwindEF.Region&amp;gt;
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; String _RegionDescription;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; RegionAdapter() { }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; RegionAdapter(NorthwindEF.Region pocoObject) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(pocoObject) { }
    &lt;span class="rem"&gt;// some methods removed...&lt;/span&gt;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DetectChanges()
    {
        &lt;span class="rem"&gt;// change detection – see below&lt;/span&gt;
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; Int32 _RegionID;
 
    [EdmScalarProperty(EntityKeyProperty=&lt;span class="kwrd"&gt;true&lt;/span&gt;, IsNullable=&lt;span class="kwrd"&gt;false&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Int32 RegionID
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; _RegionID;
        }
        set
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (_pocoEntity != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            {
               &lt;span class="rem"&gt;// pass the property value to POCO object&lt;/span&gt;
                PocoEntity.RegionID = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            }
            ReportPropertyChanging(&lt;span class="str"&gt;"RegionID"&lt;/span&gt;);
            _RegionID = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            ReportPropertyChanged(&lt;span class="str"&gt;"RegionID"&lt;/span&gt;);
        }
    }

    [EdmScalarProperty(EntityKeyProperty=&lt;span class="kwrd"&gt;false&lt;/span&gt;, IsNullable=&lt;span class="kwrd"&gt;false&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; String RegionDescription
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; _RegionDescription;
        }
        set
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (_pocoEntity != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            {
                PocoEntity.RegionDescription = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            }

            ReportPropertyChanging(&lt;span class="str"&gt;"RegionDescription"&lt;/span&gt;);
            _RegionDescription = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            ReportPropertyChanged(&lt;span class="str"&gt;"RegionDescription"&lt;/span&gt;);
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;Change detection is a step that occurs just before changes are saved to the database. Adapter objects compare their stored values and relationship information against information in POCO objects (that users may have changed). If the values differ, adapter sets its own property to the value from POCO object which essentially triggers EF change tracker and ensures that SaveChanges() will persist the object correctly. Change detection looks similar to (in reality more corner cases are covered):&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DetectChanges()
{
  &lt;span class="rem"&gt;// change detection - simplified for explanation purposes&lt;/span&gt;
  &lt;span class="kwrd"&gt;base&lt;/span&gt;.DetectChanges();
  &lt;span class="kwrd"&gt;if&lt;/span&gt; (PocoEntity.RegionID != _RegionID)
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.RegionID = PocoEntity.RegionID;
  &lt;span class="kwrd"&gt;if&lt;/span&gt; (PocoEntity.RegionDescription != _RegionDescription)
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.RegionDescription = PocoEntity.RegionDescription;
}&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'; mso-no-proof: yes"&gt;&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&gt;DetectChanges() method seems heavy and it really is – it has to store and compare all property values of all objects. Fortunately we can optimize the cost of change detection by introducing Proxies. Proxies are objects that implement POCO interface (by deriving from a POCO class), but they have a hidden dependency on an adapter object. They override all property getters and setters that can be overridden and provide just-in-time change tracking. See the following example of a proxy for Territory class that has a single virtual property called TerritoryDescription:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TerritoryProxy : NorthwindEF.Territories.Territory
{
    NorthwindEF.Territories.PocoAdapters.TerritoryAdapter _adapter;

    &lt;span class="kwrd"&gt;object&lt;/span&gt; IEntityProxy.Adapter
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _adapter; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; TerritoryProxy(TerritoryAdapter adapter) { _adapter = adapter; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; String TerritoryDescription
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._adapter.TerritoryDescription; }
        set
        {
           &lt;span class="rem"&gt;// pass the value to the POCO property&lt;/span&gt;
            &lt;span class="kwrd"&gt;base&lt;/span&gt;.TerritoryDescription = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;this&lt;/span&gt;._adapter.Context != &lt;span class="kwrd"&gt;null&lt;/span&gt; 
               &amp;amp;&amp;amp; &lt;span class="kwrd"&gt;value&lt;/span&gt; != &lt;span class="kwrd"&gt;this&lt;/span&gt;._adapter.TerritoryDescription)
            {
               &lt;span class="rem"&gt;// raise change detection event&lt;/span&gt;
              _adapter.Context.RaiseChangeDetected(&lt;span class="kwrd"&gt;this&lt;/span&gt;, 
                     &lt;span class="str"&gt;"TerritoryDescription"&lt;/span&gt;,
                     &lt;span class="kwrd"&gt;this&lt;/span&gt;._adapter.TerritoryDescription, &lt;span class="kwrd"&gt;value&lt;/span&gt;);
            }
           &lt;span class="rem"&gt;// pass the value to the Adapter which triggers change tracker&lt;/span&gt;
            &lt;span class="kwrd"&gt;this&lt;/span&gt;._adapter.TerritoryDescription = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
        }
    }
}&lt;/pre&gt;
&lt;p&gt;Adapters are not meant to be seen in any scenarios and users should not be aware of their existence. To do this, we need to expose a programmable layer that hides adapters completely. We need to be able to specify LINQ to Entities queries in terms of POCO objects (similar to ObjectQuery&amp;lt;T&amp;gt;) and we want inserts/attaches/deletes to work too. We are introducing a new object called EntitySet&amp;lt;T&amp;gt;, that combines function of ObjectQuery&amp;lt;T&amp;gt; and object-management methods (AddToX(), AttachTo(), DeleteObject()). We are also generating a replacement for ObjectContext, which makes the end-to-end programming experience with POCOs more natural. See the "Working with POCO classes” section below for more usage information.&lt;/p&gt;
&lt;h4&gt;Components&lt;/h4&gt;
&lt;p&gt;The sample includes two main components:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;EFPocoAdapter - library which you reference in your applications&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;EFPocoClassGen - code generator which generates the adapter layer&lt;/p&gt;
&lt;p&gt;To use the sample you will need five additional pieces of the puzzle:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;POCO classes (you typically write them by hand, the sample includes NorthwindEF project)&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;POCO container – root object used for querying POCO (equivalent of ObjectContext for persistence-aware objects). &lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;POCO adapter – generated&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Entity Data Model (CSDL,SSDL &amp;amp; MSL files)&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Connection string (in your App.config or in code)&lt;/p&gt;
&lt;p&gt;In a typical case you will generate POCO classes or write them by hand, but EFPocoClassGen has a mode to generate them from an existing model. &lt;/p&gt;
&lt;p&gt;POCO container can be written by hand or generated from a CSDL file. &lt;/p&gt;
&lt;p&gt;POCO Adapter for your model is generated at compile time or at runtime. In the former case, you will be able to debug the adapter code as the source code will be stored on your hard drive. In the latter case, your application will include just classes written by hand and no artificial artifacts.&lt;/p&gt;
&lt;h2 style="margin: 10pt 0in 0pt"&gt;Compile-Time Adapter Generation&lt;/h2&gt;
&lt;p&gt;EFPocoClassGen has three modes and can generate:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;POCO Adapter (default)&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;POCO container&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l3 level1 lfo2"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;POCO classes&lt;/p&gt;
&lt;p&gt;Assuming you want to generate a POCO adapter for classes that are stored in NorthwindEF.dll and your model is in NorthwindEFModel.csdl, you should run the following command:&lt;/p&gt;&lt;pre class="csharpcode"&gt;EFPocoClassGen.exe /incsdl:NorthwindEFModel.csdl /outputfile:PocoAdapter.cs 
                   /&lt;span class="kwrd"&gt;ref&lt;/span&gt;:NorthwindEF.dll /map:NorthwindEFModel=NorthwindEF&lt;/pre&gt;
&lt;p&gt;This will generate PocoAdapter.cs file based on NorthwindEFModel.csdl (specified in /incsdl option). By specifying /ref argument, you allow class generator to scan the assembly and generate certain optimizations such as proxy-based change tracking for virtual properties. This is optional but if you don’t provide the assembly references you will have to put optimization hints in the CSDL file manually. /map allows you to use a different namespace in CSDL than in your classes. Code generator has some more options; to see them all, run it without any parameters.&lt;/p&gt;
&lt;p&gt;It is usually best to automate code generation for a project by putting the appropriate commands in Pre-Build Step of VS project or in BeforeCompile target in MSBuild script. The generator will not regenerate the output file if the input hasn’t changed, so you will not get unnecessary recompiles because of that.&lt;/p&gt;
&lt;h2 style="margin: 10pt 0in 0pt"&gt;POCO Container&lt;/h2&gt;
&lt;p&gt;The POCO Container is a simple class that derives from EFPocoContext. It has a bunch of properties representing top-level entity sets. All methods that developers interact with are defined in the base class or in entity set methods.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; NorthwindEntities : EFPocoContext&amp;lt;NorthwindEF.PocoAdapters.NorthwindEntitiesAdapter&amp;gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; NorthwindEntities()
     : &lt;span class="kwrd"&gt;base&lt;/span&gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntitiesAdapter()) { }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; NorthwindEntities(&lt;span class="kwrd"&gt;string&lt;/span&gt; connectionString)
     : &lt;span class="kwrd"&gt;base&lt;/span&gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntitiesAdapter(connectionString)) { }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; NorthwindEntities(EntityConnection connection)
     : &lt;span class="kwrd"&gt;base&lt;/span&gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntitiesAdapter(connection)) { }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; IEntitySet&amp;lt;NorthwindEF.Employee&amp;gt; Employees
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; GetEntitySet&amp;lt;NorthwindEF.Employee&amp;gt;(&lt;span class="str"&gt;"Employees"&lt;/span&gt;); }
    }
 
    &lt;span class="kwrd"&gt;public&lt;/span&gt; IEntitySet&amp;lt;NorthwindEF.Order&amp;gt; Orders
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; GetEntitySet&amp;lt;NorthwindEF.Order&amp;gt;(&lt;span class="str"&gt;"Orders"&lt;/span&gt;); }
    }
 
    &lt;span class="rem"&gt;// additional entity sets removed for brevity&lt;/span&gt;
}&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;To generate POCO container, run the same command as above with /mode:PocoContainer – just be sure to specify a different output file: &lt;/p&gt;&lt;pre class="csharpcode"&gt;EFPocoClassGen.exe /mode:PocoContainer /incsdl: NorthwindEFModel.csdl /outputfile:NorthwindEntities.cs &lt;br&gt;                   /ref:NorthwindEF.dll /map:NorthwindEFModel=NorthwindEF &lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;If you prefer, you can also write the class by hand – the class needs to have as many entity set properties as you require (not all EntitySets from CSDL have to exposed) and has to have one or more of the constructors as seen above.&lt;/p&gt;
&lt;h4&gt;Setting Up the Solution&lt;/h4&gt;
&lt;p&gt;The recommended way of setting up your solution when using compile-time code generation is:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l0 level1 lfo3"&gt;&lt;span style="mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;1.&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Put all POCO classes in a separate assembly, say NorthwindEF.dll. The assembly doesn’t need to have any references (other than mscorlib.dll and maybe System.Core.dll)&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l0 level1 lfo3"&gt;&lt;span style="mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;2.&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Create a separate library project for your adapter, name it for example NorthwindEF.PocoAdapter.dll Add references to System.Data.Entity.dll, EFPocoAdapter.dll and NorthwindEF.dll&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l0 level1 lfo3"&gt;&lt;span style="mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;3.&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Set up pre-build scripts that will generate PocoAdapter.cs and NorthwindEntities.cs. Build the project once and add generated files to the project (you don’t have to add them to source control as they will be regenerated at build time)&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l0 level1 lfo3"&gt;&lt;span style="mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;4.&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;In your code that uses the POCO adapter, add references to: EFPocoAdapter.dll, NorthwindEF.dll, NorthwindEF.PocoAdapter.dll and System.Data.Entity.dll&lt;/p&gt;
&lt;p&gt;POCO Adapter can be also generated at runtime using EFPocoAdapterGenerator class:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// set up generator&lt;/span&gt;
var generator = &lt;span class="kwrd"&gt;new&lt;/span&gt; EFPocoAdapterGenerator&amp;lt;MyHandWrittenNorthwindEntities&amp;gt;();
generator.EntityConnectionString = &lt;span class="str"&gt;"name=NorthwindEntities"&lt;/span&gt;;
generator.EdmToClrNamespaceMapping[&lt;span class="str"&gt;"NorthwindEFModel"&lt;/span&gt;] = &lt;span class="str"&gt;"NorthwindEF"&lt;/span&gt;;
generator.GenerateProxies = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
generator.ObjectAssemblies.Add(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Customer).Assembly);
generator.ObjectAssemblies.Add(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Territory).Assembly);

&lt;span class="rem"&gt;// generate adapter and create context factory&lt;/span&gt;
var factory = generator.CreateContextFactory();

&lt;span class="rem"&gt;// use generated context&lt;/span&gt;
&lt;span class="kwrd"&gt;using&lt;/span&gt; (var context = factory.CreateContext())
{
    Assert.IsInstanceOfType(context, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(MyHandWrittenNorthwindEntities));
    var cust = context.Customers.First(c =&amp;gt; c.CustomerID == &lt;span class="str"&gt;"ALFKI"&lt;/span&gt;);
}
&lt;/pre&gt;
&lt;p&gt;See EFPocoAdapterGeneratorTests class for a full working example.&lt;/p&gt;
&lt;h4&gt;Working with POCO classes&lt;/h4&gt;
&lt;p&gt;Usage patterns of the generated POCO adapter layer are very similar to regular EF, but because objects are now persistence-ignorant, some APIs have been changed. This section describes important differences:&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Managing POCO Container&lt;/h3&gt;
&lt;p&gt;POCO Container should be managed in the same way as a regular ObjectContext. Same rules and recommendations apply. It is usually best to have short-living contexts and ensure proper disposal of all resources, preferably with the use of the using statement:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (NorthwindEntities context = &lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntities())
{
    &lt;span class="rem"&gt;// use context&lt;/span&gt;
}&lt;/pre&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Queries&lt;/h3&gt;
&lt;p&gt;EFPocoContext exposes CreateQuery&amp;lt;T&amp;gt; which internally wraps ObjectQuery&amp;lt;T&amp;gt;. POCO context also exposes entity sets that can be used to query objects using LINQ to Entities, ESQL builder methods and Entity SQL. Of course, you can still use EntityClient and EntityCommand to do queries in the value layer.&lt;/p&gt;
&lt;p&gt;In short: queries look exactly the same as in EF, just entity types and query object types are different.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (NorthwindEntities context = &lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntities())
{
    Customer k = (from c &lt;span class="kwrd"&gt;in&lt;/span&gt; context.Customers 
                  &lt;span class="kwrd"&gt;where&lt;/span&gt; c.CustomerID == &lt;span class="str"&gt;"ALFKI"&lt;/span&gt;
                  select c).First();
    Console.WriteLine(k.Address.City);
}&lt;/pre&gt;
&lt;p&gt;See QueryTests.cs and BuilderMethodsTests.cs for more examples of usage. &lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Inserting, Removing and Attaching Objects&lt;/h3&gt;
&lt;p&gt;POCO context code exposes IEntitySet&amp;lt;T&amp;gt; properties that can be used to query (because they derive from IQueryable&amp;lt;T&amp;gt;) but also insert, remove and attach objects to the context. This is a change from EF where you used to use AddObject(), AttachTo() and DeleteObject() on ObjectContext class. The idea of exposing EntitySet&amp;lt;T&amp;gt; object to aggregate all operations to entity sets was originally introduced in the &lt;a href="http://code.msdn.microsoft.com/EFExtensions"&gt;EFExtensions&lt;/a&gt; project:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (NorthwindEntities context = &lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntities(conn))
{
  &lt;span class="rem"&gt;// add new object&lt;/span&gt;
  context.Customers.InsertOnSaveChanges(&lt;span class="kwrd"&gt;new&lt;/span&gt; Customer { 
      CustomerID = &lt;span class="str"&gt;"YYYY"&lt;/span&gt;, 
      CompanyName = &lt;span class="str"&gt;"Una Firma"&lt;/span&gt; 
  });

  &lt;span class="rem"&gt;// attach existing object for tracking&lt;/span&gt;
  var product = &lt;span class="kwrd"&gt;new&lt;/span&gt; Product(&lt;span class="kwrd"&gt;new&lt;/span&gt; Supplier()) { ProductID = 1 };
  context.Products.Attach(product);

  &lt;span class="rem"&gt;// locate and delete object&lt;/span&gt;
  var cust = context.Customers.First(c =&amp;gt; c.CustomerID == &lt;span class="str"&gt;"XXXX"&lt;/span&gt;);
  context.Customers.DeleteOnSaveChanges(cust);

  &lt;span class="rem"&gt;// persist changes&lt;/span&gt;
  context.SaveChanges();
}&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;See EntitySetTests.cs and CRUDTests.cs for more examples.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Transparent Lazy Loading&lt;/h3&gt;
&lt;p&gt;Lazy loading happens automatically when you reference a virtual property on an entity object that was retrieved from a query. Because the property is marked as virtual, EFPocoClassGen generates a derived class called a proxy, which overrides the property and provides lazy-loading behavior on it.&lt;/p&gt;
&lt;p&gt;Similar thing happens for collections – they get loaded on first access, but in this case the collection accessor doesn’t have to be declared virtual but its type must be an interface (ICollection&amp;lt;T&amp;gt;, IList&amp;lt;T&amp;gt;) instead of a concrete class (such as List&amp;lt;T&amp;gt;). In this case POCO adapter initializes the collection with a tracked collection that provides transparent lazy loading on enumeration and other types of access.&lt;/p&gt;
&lt;p&gt;You can disable this lazy loading behavior at runtime, by setting EFPocoContext.EnableLazyLoading property to false or at compile time, by passing /enableLazyLoading:false to EFPocoClassGen. Note that if compile your adapter classes without lazy loading support, you won’t be able to enable lazy loading at runtime.&lt;/p&gt;
&lt;p&gt;If the lazy loading has been disabled, you can still load a reference or collection you want to navigate to by using new methods on EFPocoContext:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;IsPropertyLoaded(o,p) – determines whether object or collection represented by property p on object o has been loaded. Property can be specified as a string or as a strongly typed lambda.&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;LoadProperty(o,p) – loads the related end of the navigation property p on object o. Property can be specified as a string or as a strongly typed lambda.&lt;/p&gt;
&lt;p&gt;See LazyLoadingTests.cs for example usage.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Object-to-Conceptual Mapping&lt;/h3&gt;
&lt;p&gt;You can specify the namespace mapping (from conceptual space to CLR space) when generating POCO adapter using /map parameter to EFPocoClassGen. If you need to fine-tune this (for example to materialize a class from another assembly or namespace), you can attach an attribute called ClrType to &amp;lt;EntityType&amp;gt; or &amp;lt;ComplexType&amp;gt; in your CSDL schema.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;EntityType&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="CurrentEmployee"&lt;/span&gt; &lt;span class="attr"&gt;BaseType&lt;/span&gt; &lt;span class="kwrd"&gt;="NorthwindEFModel.Employee"&lt;/span&gt; &lt;br&gt;            &lt;span class="attr"&gt;objectmapping:ClrType&lt;/span&gt;&lt;span class="kwrd"&gt;="Some.Clr.Namespace.CurrentEmployee"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;The sample comes with NorthwindEF schema split into two assemblies: NorthwindEF and NorthwindEF.Territories.dll and classes live in three namespaces to demonstrate this concept. See NorthwindEFModel.csdl for details.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Read-Only Value Objects&lt;/h3&gt;
&lt;p&gt;You can map your entities to classes that are read-only and all they do is a public constructor that initializes all properties. You can also have some properties read-only and others settable. All you have to do to get this to work is to use a constructor parameter naming convention. If the property is read-only, the adapter will pass its initial value as the corresponding constructor argument:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;struct&lt;/span&gt; CommonAddress
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _address;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _city;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _region;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _postalCode;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; _country;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; CommonAddress(&lt;span class="kwrd"&gt;string&lt;/span&gt; address, &lt;span class="kwrd"&gt;string&lt;/span&gt; city, &lt;span class="kwrd"&gt;string&lt;/span&gt; region, 
                         &lt;span class="kwrd"&gt;string&lt;/span&gt; postalCode, &lt;span class="kwrd"&gt;string&lt;/span&gt; country)
    {
        _address = address;
        _city = city;
        _region = region;
        _postalCode = postalCode;
        _country = country;
    }
 
    &lt;span class="kwrd"&gt;public&lt;/span&gt; String Address
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _address; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; String City
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _city; }
    }
 
    &lt;span class="kwrd"&gt;public&lt;/span&gt; String Region
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _region; }
    }
 
    &lt;span class="kwrd"&gt;public&lt;/span&gt; String PostalCode
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _postalCode; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; String Country
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _country; }
    }
}

&lt;/pre&gt;
&lt;p&gt;See CommonAddress.cs, Product.cs and Category.cs for more examples of usage.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Change Tracking&lt;/h3&gt;
&lt;p&gt;Change tracking for scalar properties and complex types is implemented in one of two modes: snapshot-based or proxy-based. Snapshot-based change tracking means that when EF materializes an object, the adapter stores a copy of its field values for further comparison. This way it knows which fields have changes and can notify object state manager accordingly.&lt;/p&gt;
&lt;p&gt;Proxy-based change tracking is used when you declare your POCO object property to be virtual. EFPocoClassGen generates a proxy class that overrides all virtual properties in your POCO classes and injects notifications that update object state manager just-in-time.&lt;/p&gt;
&lt;p&gt;Change tracking for collections can also use snapshots or proxies – in the first case EntityCollection&amp;lt;T&amp;gt; managed by ObjectStateManager is used as original values, if proxies are used (only possible when collection formal type is ICollection&amp;lt;T&amp;gt; or IList&amp;lt;T&amp;gt;) the Object State Manager gets updated just-in-time.&lt;/p&gt;
&lt;p&gt;Change detection happens just before changes are saved to the database. You can force it by calling DetectChanges() on ObjectStateManager. Forcing change detection can be useful to do relationship fixup before changes are saved. It also raises change tracking events.&lt;/p&gt;
&lt;p&gt;There are 2 change tracking events on EFPocoContext:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;ChangeDetected that gets raised whenever a change is detected on a scalar, complex type property or a relationship where related end is of single cardinality&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;CollectionChangeDetected that gets raised whenever a change in collection contents is detected&lt;/p&gt;
&lt;p&gt;You can hook up those events for debugging purposes, but there may be other possible uses, such as auditing or tracing.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Compiled Queries&lt;/h3&gt;
&lt;p&gt;Compiled queries work in the same way as in EF, except that you use EFPocoCompiledQuery.Compile() instead of CompiledQuery.Compile()&lt;/p&gt;&lt;pre class="csharpcode"&gt;var getCustomerByID = EFPocoCompiledQuery.Compile(
   (NorthwindEntities context, &lt;span class="kwrd"&gt;string&lt;/span&gt; customerID) =&amp;gt;
       context.Customers.Where(c =&amp;gt; c.CustomerID == customerID).First());

&lt;span class="kwrd"&gt;using&lt;/span&gt; (NorthwindEntities context = &lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntities())
{
    var alfki = getCustomerByID(context, &lt;span class="str"&gt;"ALFKI"&lt;/span&gt;);
}&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;See CompiledQueryTests.cs for more information.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Working With Proxies&lt;/h3&gt;
&lt;p&gt;Proxies are injected automatically when EFPocoAdapter materializes new POCO objects. This applies to all objects being results of queries and does NOT apply to objects passed by users.&lt;/p&gt;
&lt;p&gt;Users can force a proxy to be created by calling EFPocoContext.CreateObject&amp;lt;T&amp;gt; instead of new T().&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (NorthwindEntities context = &lt;span class="kwrd"&gt;new&lt;/span&gt; NorthwindEntities())
{
    var cust = context.CreateObject&amp;lt;Customer&amp;gt;();
    Assert.IsTrue(context.IsProxy(cust));
}&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;Users can also enable and disable proxy use (which effectively disables transparend lazy loading) by setting EFPocoContext.EnableChangeTrackingUsingProxies property to false. Note that this applies to newly materialized objects, not to objects that have already been created and managed by the context. You can convert your existing POCO objects to Proxies with and back with ConvertPocoToProxies() and ConvertProxiesToPoco() methods accordingly.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Shadow State&lt;/h3&gt;
&lt;p&gt;Sometimes we want to hide certain properties from POCO objects and let the framework managed them. For example, we typically don’t need to see GUIDs, timestamp or version values used for optimistic concurrency checks as long as the framework can manage them automatically. The state for those properties can still be maintained by the adapter, but the property doesn’t have to be present on the POCO object.&lt;/p&gt;
&lt;p&gt;In the similar manner we may want to hide primary keys if they don’t have business value, provided that there you have defined your column as IDENTITY. &lt;/p&gt;
&lt;p&gt;For example, the following POCO class:&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; Territory
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; TerritoryDescription { get; set; }
}&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Is declared in CSDL as:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;EntityType&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="Territory"&lt;/span&gt; &lt;span class="attr"&gt;objectmapping:ClrType&lt;/span&gt;&lt;span class="kwrd"&gt;="NorthwindEF.Territories.Territory"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Key&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PropertyRef&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="TerritoryID"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Key&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Property&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="TerritoryID"&lt;/span&gt; &lt;span class="attr"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;="Int32"&lt;/span&gt; &lt;span class="attr"&gt;Nullable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt;
            &lt;span class="attr"&gt;objectmapping:ChangeTracking&lt;/span&gt;&lt;span class="kwrd"&gt;="Hidden"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Property&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;="TerritoryDescription"&lt;/span&gt; &lt;span class="attr"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;="String"&lt;/span&gt; &lt;span class="attr"&gt;Nullable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;EntityType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note the use of &lt;span style="font-size: 10pt; color: red; line-height: 115%; font-family: 'Courier New'; mso-no-proof: yes"&gt;objectmapping:ChangeTracking&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; line-height: 115%; font-family: 'Courier New'; mso-no-proof: yes"&gt;=&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; mso-no-proof: yes"&gt;"&lt;span style="color: blue"&gt;Hidden&lt;/span&gt;"&lt;/span&gt; to notify the code generator that the property is not present on the POCO class. This is optional if you use /ref: as in this case the code generator can detect that the property is missing and automatically assume “shadow” property.&lt;/p&gt;
&lt;h3 style="margin: 10pt 0in 0pt"&gt;Other Properties of EFPocoContext &lt;/h3&gt;
&lt;p&gt;EFPocoContext exposes several helper properties, similar to ObjectContext:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;MetadataWorkspace – gives you access to metadata workspace for the model&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;QueryProvider – exposes IQueryProvider used to translate LINQ queries to the form that Linq To Entities understands&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Connection – gives you access to underlying EntityConnection&lt;/p&gt;
&lt;h2 style="margin: 10pt 0in 0pt"&gt;Limitations&lt;/h2&gt;
&lt;p&gt;The first version of the sample has certain limitations. Not all APIs of EF are wrapped and exposed in a POCO-friendly manner. You can still access the following APIs through adapter objects:&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;ObjectStateManager/ObjectStateEntry&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="margin: 0in 0in 0pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Query modes other than AppendOnly&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="margin: 0in 0in 10pt 0.5in; text-indent: -0.25in; mso-list: l2 level1 lfo4"&gt;&lt;span style="font-family: symbol; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol"&gt;&lt;span style="mso-list: ignore"&gt;·&lt;span style="font: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;ObjectResult and other databinding-specific classes&lt;/p&gt;
&lt;p&gt;It should be relatively straightforward to add the wrappers as needed using techniques described in the article.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8936938" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jkowalski/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/jkowalski/archive/tags/EFPocoAdapter/default.aspx">EFPocoAdapter</category></item></channel></rss>