<?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>Entity Framework Design : ObjectServices</title><link>http://blogs.msdn.com/efdesign/archive/tags/ObjectServices/default.aspx</link><description>Tags: ObjectServices</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Code Only</title><link>http://blogs.msdn.com/efdesign/archive/2009/06/10/code-only.aspx</link><pubDate>Wed, 10 Jun 2009 23:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9724905</guid><dc:creator>efdesign</dc:creator><slash:comments>27</slash:comments><comments>http://blogs.msdn.com/efdesign/comments/9724905.aspx</comments><wfw:commentRss>http://blogs.msdn.com/efdesign/commentrss.aspx?PostID=9724905</wfw:commentRss><description>&lt;P&gt;There are currently two ways to get Entity Framework apps up and running, we call these Database First and Model First respectively. &lt;/P&gt;
&lt;P&gt;Database First has been with us since .NET 3.5 SP1, and involves reverse engineering an Entity Data Model from an existing database.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/adonet/archive/2009/05/12/sneak-preview-model-first-in-the-entity-framework-4-0.aspx" mce_href="http://blogs.msdn.com/adonet/archive/2009/05/12/sneak-preview-model-first-in-the-entity-framework-4-0.aspx"&gt;Model First&lt;/A&gt; is new to Visual Studio 2010/.NET 4.0 and allows you to create an Entity Data Model from scratch and then generate a database and mapping for it.&lt;/P&gt;
&lt;P&gt;However many developers view their Code as their model. &lt;/P&gt;
&lt;P&gt;Ideally these developers just want to write some Domain classes and without ever touching a designer or a piece of XML be able to use those classes with the Entity Framework. &lt;BR&gt;Basically they want to write ‘Code Only’.&lt;/P&gt;
&lt;P&gt;We are pleased to announce that we’ll be shipping a preview of “Code Only” on top of the .NET Framework 4.0 Beta 1 (more on that in the coming days).&lt;/P&gt;
&lt;H5&gt;How it works:&lt;/H5&gt;
&lt;P&gt;To use Code only you simply write some &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 classes&lt;/A&gt;, here is a simple example:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;public class Category &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp; public int ID { get; set; } &lt;BR&gt;&amp;nbsp;&amp;nbsp; public string Name { get; set; } &lt;BR&gt;&amp;nbsp;&amp;nbsp; public List&amp;lt;Product&amp;gt; Products { get; set; } &lt;BR&gt;} &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;public class Product &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp; public int ID { get; set; } &lt;BR&gt;&amp;nbsp;&amp;nbsp; public string Name { get; set; } &lt;BR&gt;&amp;nbsp;&amp;nbsp; public Category Category { get; set; } &lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Then write a class that derives from &lt;FONT face="Courier New"&gt;ObjectContext&lt;/FONT&gt; that describes the shape of your model and how you want to access your POCO classes, so something like this: &lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;public class MyContext : ObjectContext &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp; public MyContext(EntityConnection conn) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; : base(conn){ }&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp; public ObjectSet&amp;lt;Category&amp;gt; Categories { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return base.CreateObjectSet&amp;lt;Category&amp;gt;(); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp; public ObjectSet&amp;lt;Product&amp;gt; Products { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return base.CreateObjectSet&amp;lt;Product&amp;gt;(); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;NOTE:&lt;/STRONG&gt; this persistence / EF aware class could easily be in another assembly so you can keep it separate from your nice pristine domain classes.&lt;/P&gt;
&lt;P&gt;At this point you have everything you need from a CLR perspective, but you won’t be able to use the &lt;FONT face="Courier New"&gt;MyContext&lt;/FONT&gt; class without some EF metadata, which is normally stored in the EDMX file. &lt;/P&gt;
&lt;P&gt;But remember Code-Only means there isn’t an EDMX file. &lt;BR&gt;So where do we get the metadata? &lt;BR&gt;That is where the &lt;FONT face="Courier New"&gt;ContextBuilder&lt;/FONT&gt; class comes in:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;SqlConnection connection = …; // some un-opened SqlConnection &lt;BR&gt;MyContext context = ContextBuilder.Create&amp;lt;MyContext&amp;gt;(connection);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;What is happening here is that the &lt;FONT face="Courier New"&gt;ContextBuilder&lt;/FONT&gt; is looking at the Properties on &lt;FONT face="Courier New"&gt;MyContext&lt;/FONT&gt;, inferring a default Conceptual Model, Storage Model and Mapping by convention, it the uses that metadata plus the &lt;FONT face="Courier New"&gt;SqlConnection&lt;/FONT&gt; you passed in to create an &lt;FONT face="Courier New"&gt;EntityConnection&lt;/FONT&gt;, and finally it constructs an instance of &lt;FONT face="Courier New"&gt;MyContext&lt;/FONT&gt; by passing the &lt;FONT face="Courier New"&gt;EntityConnection&lt;/FONT&gt; to the constructor we created previously.&lt;/P&gt;
&lt;P&gt;Once you have an instance of your context, we ship several extension methods you can use to automatically create a database script, see if the database exists, drops the database, and/or create the database,. For example this code snippet uses two of those extension methods to create the database if it doesn’t already exist.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;if (!context.DatabaseExists()) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.CreateDatabase();&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;The &lt;FONT face="Courier New"&gt;CreateDatabase&lt;/FONT&gt; call looks at the EF metadata, in particular the Storage Model, aka SSDL, and uses it to produce Database Definition Language (or DDL) which it then executes against against the connection. &lt;/P&gt;
&lt;H5&gt;Overriding Conventions&lt;/H5&gt;
&lt;P&gt;In the examples so far everything is done by convention. This is great if you are happy with our conventions. However you may want to override them, for example to register different keys, use different mappings or use a different inheritance strategy etc.&lt;/P&gt;
&lt;P&gt;To override the convention instead of creating an &lt;FONT face="Courier New"&gt;ObjectContext&lt;/FONT&gt; directly you create an instance of a &lt;FONT face="Courier New"&gt;ContextBuilder&lt;/FONT&gt; that you can configure. &lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;var contextBuilder = new ContextBuilder&amp;lt;MyContext&amp;gt;();&lt;/FONT&gt; &lt;BR&gt;&lt;BR&gt;In the first Feature CTP, Code Only provides the ability to override how the key properties are inferred:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;contextBuilder.RegisterKey((Product p) =&amp;gt; p.ID);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;This code tells the builder that the key of &lt;FONT face="Courier New"&gt;Product&lt;/FONT&gt; is its &lt;FONT face="Courier New"&gt;ID&lt;/FONT&gt; property.&lt;/P&gt;
&lt;P&gt;When you’ve finished configuring how you want your model and mappings to work in your code, you create an instance of your context, this time by calling Create on the builder instance you have been configuring:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;MyContext context = contextBuilder.Create(connection);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;It is that simple.&lt;/P&gt;
&lt;P&gt;Over time the features of the &lt;FONT face="Courier New"&gt;ContextBuilder&lt;/FONT&gt; will grow so you will be able to setup custom mappings, mark properties as transient, setup up Facets (e.g. &lt;FONT face="Courier New"&gt;MaxLength&lt;/FONT&gt;), change inheritance strategies, link properties that are the inverse of each other together (e.g. &lt;FONT face="Courier New"&gt;product.Category&lt;/FONT&gt; and &lt;FONT face="Courier New"&gt;category.Products&lt;/FONT&gt;) and more. In fact here is an example of the sort of thing we are aiming to support for the next release of the Feature CTP: &lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;builder[“dbo.prds”] =&amp;nbsp; &lt;BR&gt;&amp;nbsp; from c in builder.OfType&amp;lt;Product&amp;gt;() &lt;BR&gt;&amp;nbsp; select new { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pcode = p.ID, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Name, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cat = p.Category.ID &lt;BR&gt;&amp;nbsp; } &lt;BR&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;This snippet:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Maps &lt;FONT face="Courier New"&gt;Product&lt;/FONT&gt; entities to the ‘&lt;FONT face="Courier New"&gt;prds&lt;/FONT&gt;’ table. &lt;/LI&gt;
&lt;LI&gt;Maps &lt;FONT face="Courier New"&gt;Product.ID&lt;/FONT&gt; to a ‘&lt;FONT face="Courier New"&gt;pcode&lt;/FONT&gt;’ column. &lt;/LI&gt;
&lt;LI&gt;Maps &lt;FONT face="Courier New"&gt;Product.Name&lt;/FONT&gt; to a ‘&lt;FONT face="Courier New"&gt;Name&lt;/FONT&gt;’ column. &lt;/LI&gt;
&lt;LI&gt;Maps the FK under the &lt;FONT face="Courier New"&gt;Product.Category&lt;/FONT&gt; relationship to the ‘&lt;FONT face="Courier New"&gt;cat&lt;/FONT&gt;’ column. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Here is another example that does basic &lt;A href="http://blogs.msdn.com/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx" mce_href="http://blogs.msdn.com/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx"&gt;TPH&lt;/A&gt; inheritance:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;builder[“dbo.Cars”] = ( &lt;BR&gt;&amp;nbsp; from b in builder.OfTypeOnly&amp;lt;Car&amp;gt;() &lt;BR&gt;&amp;nbsp; select new { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id = b.CID.AsKey(), &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b.Color, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b.Manufacturer, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; disc = “C” &lt;BR&gt;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;).Merge( &lt;BR&gt;&amp;nbsp; from s in builder.OfType&amp;lt;SportsCar&amp;gt;() &lt;BR&gt;&amp;nbsp; select new { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id = s.CID.AsKey(),&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s.Color,&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s.Manufacturer,&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; disc = “S”,&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hp = s.HorsePower,&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tq = s.Torque &lt;BR&gt;&amp;nbsp; } &lt;BR&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;These two examples just scratch the surface of what we are planning on doing, hopefully though they give you a sense of where we are heading. &lt;/P&gt;
&lt;P&gt;We are super excited about Code Only. But as of right now our plans to support overriding conventions aren’t finalized, so we’d love to get your feedback, this really is your chance to help us get it right.&lt;/P&gt;
&lt;P&gt;&lt;B&gt;&lt;A href="http://blogs.msdn.com/alexj" mce_href="http://blogs.msdn.com/alexj"&gt;Alex James&lt;/A&gt;&lt;/B&gt;, &lt;BR&gt;Program Manager, Entity Framework Team, Microsoft &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;B&gt;This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at &lt;/B&gt;&lt;/EM&gt;&lt;A href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx"&gt;&lt;EM&gt;&lt;B&gt;this post&lt;/B&gt;&lt;/EM&gt;&lt;/A&gt;&lt;EM&gt;&lt;B&gt;. &lt;/B&gt;&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9724905" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Mapping/default.aspx">Mapping</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/ObjectServices/default.aspx">ObjectServices</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/CodeOnly/default.aspx">CodeOnly</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework+Futures/default.aspx">Entity Framework Futures</category></item><item><title>N-Tier Improvements for Entity Framework</title><link>http://blogs.msdn.com/efdesign/archive/2008/11/20/n-tier-improvements-for-entity-framework.aspx</link><pubDate>Thu, 20 Nov 2008 08:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9127479</guid><dc:creator>efdesign</dc:creator><slash:comments>51</slash:comments><comments>http://blogs.msdn.com/efdesign/comments/9127479.aspx</comments><wfw:commentRss>http://blogs.msdn.com/efdesign/commentrss.aspx?PostID=9127479</wfw:commentRss><description>&lt;P mce_keep="true"&gt;The first version of Entity Framework provides convenient ways to load, manipulate and persist objects and relationships. As with many other O/RMs, Entity Framework has a state manager that tracks every change made. Existing objects are typically loaded first from the database, later modified, and finally the changes are saved back to the store. &lt;/P&gt;
&lt;P&gt;Another feature, full graph serialization, makes it very easy for developers to ship around object graphs representing snapshots of the &lt;B&gt;current state&lt;/B&gt; of the world, across execution boundaries. &lt;/P&gt;
&lt;P&gt;The next version of Entity Framework will also support Persistence Ignorance. Therefore object graphs can now be made of &lt;A href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object" mce_href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object"&gt;POCO&lt;/A&gt; instances, which &lt;A href="http://www.pluralsight.com/community/blogs/aaron/archive/2008/05/13/50934.aspx" mce_href="http://www.pluralsight.com/community/blogs/aaron/archive/2008/05/13/50934.aspx"&gt;WCF now also supports&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Nonetheless, there is a task that belongs in any N-Tier application that still requires a great amount of work for developers using EF:&amp;nbsp; decoding messages that represent &lt;B&gt;state changes&lt;/B&gt; performed by the client, meant to be processed or persisted by the service. &lt;/P&gt;
&lt;P&gt;The major pain point in the first version is the number of intricate steps a program needs to perform in order to setup the state manager in the appropriate state according to the changes encoded in the message. For instance, for any but the simplest scenarios, in order to use the basic graph manipulations APIs in ObjectContext, like AddObject and AttachTo, it is necessary to “shred” the object graphs contained in the message, which destroys important information about relationships between object instances that therefore has to be kept somewhere else (as an illustration of this approach, see Danny Simmons’s EntityBag in project &lt;A href="http://code.msdn.microsoft.com/entitybag" mce_href="http://code.msdn.microsoft.com/entitybag"&gt;Perseus&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;In addition, given that the message typically contains all the necessary information and that concurrency is usually a concern, the program shouldn’t have to re-query the database. Instead, it should be easier to “tell” the state manager about the original state (that came from the database previously) and about what changes were made.&lt;/P&gt;
&lt;P&gt;There are a few approaches to N-Tier that are very popular in the industry. Customers using Entity Framework for the first time often carry the expectation that it will support a similar experience:&lt;/P&gt;
&lt;H3&gt;DataSet &lt;/H3&gt;
&lt;P&gt;The original ADO.NET data access technology includes the DataSet, a versatile data cache that can be serialized as XML and be used to transport collections of rows and relationships and, also &lt;A href="http://msdn.microsoft.com/en-us/library/ms172088(VS.71).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms172088(VS.71).aspx"&gt;diffgrams&lt;/A&gt; representing changes. &lt;/P&gt;
&lt;P&gt;DataSets make many N-Tier scenarios extremely easy to implement, and over time, have proven helpful for many customers. In spite of this, they are not appropriate for some scenarios and architectural patterns:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;P&gt;The lack of a widely accepted standard serialization format has hindered the development of implementations that could produce or consume datasets in other platforms.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;Diffgrams represent sets of CREATE, UPDATE and DELETE operations of arbitrary depth, which is not adequate for architectural patterns that conceive operations with more constrained semantics (i.e. PlaceOrder, CreateCustomer).&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;No simple ways to control the application of batch CUD operations contained in diffgrams makes them unsuitable any time there is a trust boundary between the client and the server. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;Also, the data-centered perspective of DataSets leads to an anti-pattern known as the “anemic domain model”, which is characterized by a separation between the data aspect and the behavioral aspect of domain objects.&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;H3&gt;Data Transfer Objects&lt;/H3&gt;
&lt;P&gt;The approach typically used in the DDD and SOA communities to address&amp;nbsp; multi-tier scenarios is to hand-build a service with well defined operation semantics together with the a conceptual message format, usually composed of &lt;A href="http://msdn.microsoft.com/en-us/library/ms978717.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms978717.aspx"&gt;data transfer objects&lt;/A&gt; (DTOs).&amp;nbsp; Implementing DTOs often also involves writing the logic that translates DTOs back and forth to persistent objects.&lt;/P&gt;
&lt;P&gt;Here is a simplified set of rules that characterizes the use of DTOs: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;P&gt;Actual entities are not serialized, just DTOs, which are value-only objects (without behavior) are exposed in the boundaries.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;The client does not depend on the types defined on the server. The shapes of the DTOs are the sole contract, and the types used in the client are for the client only.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;The context or container is not sent together with the message.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;The service decides what to do with the contents of the message. It is not the message who decides (as an extreme counter example, imagine a message that tells the service to delete all the contents of the database).&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Those rules are especially important in SOA scenarios, in which the client and the server can be separated by a trust boundary, such as the one that exists between two different companies. &lt;/P&gt;
&lt;P&gt;There are other permutations of layered architectures, though, in which the level of decoupling that DTOs provide may not justify the increased complexity, for example: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;P&gt;A multi-tier application in which the client is fully trusted.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;A rich internet application in which an infrastructure service is purely used for data persistence (is by design a CRUD-only service) and therefore types exposed do not contain behavior anyway.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;Any architecture in which the persistence framework is actually used to provide and manage DTOs and not the actual entities. &lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;H3&gt;REST&lt;/H3&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" mce_href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;ADO.NET Data Services&lt;/A&gt; provides a rich framework that developers can easily use to expose any Entity Data Model as a REST-style resource collection. Simple HTTP verbs like GET, PUT, POST and DELETE are used in combination with URIs to represent CRUD operations against the persistence service.&lt;/P&gt;
&lt;P&gt;This pattern is suitable for many applications and can easily be combined with service operations with richer semantics in the same application. &lt;/P&gt;
&lt;P&gt;ADO.NET Data Services, however, may not be compelling for those customers who want to define their services as operations with richer semantics entirely. &lt;/P&gt;
&lt;H2&gt;Goals&lt;/H2&gt;
&lt;P&gt;ADO.NET Data Services goes a long way addressing the needs of customers doing RESTful services, and so there is no need for us to improve Entity Framework experience on that space.&lt;/P&gt;
&lt;P&gt;With the N-Tier improvements for Entity Framework, we want to address some of the same problem space as DataSet, but we want to avoid the primary issues with it. &lt;/P&gt;
&lt;P&gt;Ideally, we would like to provide building blocks that are appealing for developers building solutions on a wide range of architectures. For instance, we would like to provide fine enough control for DTO proponents, but at the same time reduce the level of pain that those trying to address simpler scenarios experience today. &lt;/P&gt;
&lt;H2&gt;Design&lt;/H2&gt;
&lt;P&gt;If we just wanted to provide a DataSet like experience on top of Entity Framework, the most straightforward (although very expensive to test) way would probably be based on full serialization of the state manager. Basically, the internal representation of ObjectStateManager includes collections of objects to be deleted, inserted, and modified, and also relationships to be added and deleted, as well as objects and relationships that should remain unchanged. &lt;/P&gt;
&lt;P&gt;Such a model would lead to very fine grain control of the state manager, but you would lose some of the benefits of operating on the domain objects and the higher level APIs. For instance, it would make it extremely easy for the program to set the state manager in a completely invalid state.&lt;/P&gt;
&lt;P&gt;An alternative representation for such an experience would be to ship a “change log” containing the history of all operations performed on the client. This representation has the disadvantage of being more verbose than the previous one, but it would make “point in time” rollbacks pretty easy to implement.&lt;/P&gt;
&lt;P&gt;Besides these two, there are a few more interesting generic representations for changes in a graph, but in general, they all suffer from the same disadvantage: providing a solution for them does not give the user the level of control that the most complicated scenarios and sophisticated patterns require. &lt;/P&gt;
&lt;P&gt;Based on this fact, we decided to adopt the following goal for the design: &lt;/P&gt;
&lt;P&gt;&lt;B&gt;Entity Framework won’t define its own unique representation for the set of changes represented in an N-Tier application. Instead, it will provide basic building block APIs that will facilitate the use of a wide range of representations. &lt;/B&gt;&lt;/P&gt;
&lt;P&gt;This has the desirable consequence that Entity Framework won’t impose a pattern for N-Tier. Both DTO-style and DataSet-like experiences can be built on top of a minimal set of building blocks APIs. It is up to the developer to select the pattern that better suits the application.&lt;/P&gt;
&lt;P&gt;Here is a first cut of the new methods that we are proposing to add to the ObjectContext. The new APIs will work with EntityObjects, IPOCO and POCO entities:&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&lt;SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ObjectContext&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;{&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; Apply property values to original state of the entity&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="entitySetName"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;name of EntitySet of root&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="original"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;entity with original values&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ApplyOriginalValues(&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string&lt;/SPAN&gt; entitySetName, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object&lt;/SPAN&gt; original) { }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; Changes the state of the entity and incident relationships &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="entity"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;entity to change state of&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="state"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;new state&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ChangeObjectState(&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object&lt;/SPAN&gt; entity, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityState&lt;/SPAN&gt; state) { }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; Changes state of relationship &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="source"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;source object of relationship&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="target"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;target object of relationship&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="relationshipName"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;name of relationship&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="sourceRole"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;role of source object&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="targetRole"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;role of target object&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt; &amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="state"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;new state&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ChangeRelationshipState(&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object&lt;/SPAN&gt; source, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object&lt;/SPAN&gt; target, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; relationshipName, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string&lt;/SPAN&gt; sourceRole, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string&lt;/SPAN&gt; targetRole, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;EntityState&lt;/SPAN&gt; state) { } &lt;BR&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; Changes state of relationship represented by navigation &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt; property&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="source"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;source object of relationship&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="target"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;target object of relationship&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="navigationProperty"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;navigation property&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="state"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;new state&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ChangeRelationshipState(&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object&lt;/SPAN&gt; source, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object&lt;/SPAN&gt; target, &lt;BR&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string&lt;/SPAN&gt; navigationProperty, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityState&lt;/SPAN&gt; state) { }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; Changes state of relationships represented by lambda &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt; expression&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;typeparam name="TSource"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;type of source entity&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/typeparam&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="source"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;source entity of relationship&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="target"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;target entity of relationship&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="selector"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;property selector expression&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="state"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;new state&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ChangeRelationshipState&amp;lt;TSource&amp;gt;( &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TSource source, &lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object&lt;/SPAN&gt; target, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Expression&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;&amp;gt; selector, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;EntityState&lt;/SPAN&gt; state) { }&lt;/SPAN&gt; &lt;BR&gt;&amp;nbsp; &lt;BR&gt;&lt;SPAN&gt;}&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;H3&gt;ApplyOriginalValues&lt;/H3&gt;
&lt;P&gt;Similar to the existing ApplyPropertyChanges, but this API keeps the tracked entity intact, only affecting the corresponding original values. This operation only works for entities in Modified, Unchanged or Deleted states. Added entities by definition don’t have original state.&lt;/P&gt;
&lt;H3&gt;ChangeObjectState&lt;/H3&gt;
&lt;P&gt;This method will transition the state of a tracked entity passed as argument, but will also have side-effects on incident relationships. In case the new state is modified, it will also mark all properties as modified, regardless of the original and current values.&lt;/P&gt;
&lt;H3&gt;ChangeRelationshipState&lt;/H3&gt;
&lt;P&gt;Similar to ChangeObjectState, but sets the relationship between two tracked entities to the provided state. Relationships cannot be in the modified state, but this method can be used to report which relationship should be added, deleted, or unchanged.&lt;/P&gt;
&lt;P&gt;The relationship doesn’t need to exist in the context, but the entities do. If the relationship doesn’t exist, it can be created in the new state. For instance, when this code is invoked, a new relationship can be created between customer1 and order1 in the added state:&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN&gt;context.ChangeRelationship( &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; customer1, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; order1, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; c=&amp;gt;c.Orders, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EntityState&lt;/SPAN&gt;&lt;SPAN&gt;.Added);&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;P&gt;The relationship is typically represented by the navigation property but the metadata names of the relationship and roles can also be used in a different overload. &lt;/P&gt;
&lt;H3&gt;Design notes&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P&gt;All parameters named “entity”, “source” and “target” accept either entities or EntityKeys.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;We could choose to deprecate the ApplyPropertyChanges API and replace it with ApplyCurrentValues, which is more consistent name-wise with the new ApplyOriginalValues method.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;For convenience, we should add similar APIs in other types like ObjectStateEntry, RelatedEnd and others as appropriate. For instance, the ObjectStateEntry will also have a ChangeState&amp;nbsp; API that will only take the target state parameter.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;?xml:namespace prefix = o /&gt;&lt;o:p&gt;We could choose to put ChangeObjectState() and the various ChangeRelationshipState() in ObjectStateManager rather than in ObjectContext, since this is a lower level API than everything else currently in the ObjectContext.&lt;/o:p&gt;&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;H2&gt;Scenario tests&lt;/H2&gt;
&lt;P&gt;To get a sense of how the new API can be used, we have tried several different approaches and representations. We also have plans to release sample code that will show in more detail different ways to use the new API. In the meanwhile, here are a few simple cases.&lt;/P&gt;
&lt;H3&gt;General purpose Attach with state resolution delegate&lt;/H3&gt;
&lt;P&gt;In this scenario between a client and mid-tier, the intention is to allow a client to query for a Customer entity with the Customer’s Order entities. The client then makes a variety of changes to the Customer entity graph including modifying the Customer, and adding or removing Orders.&amp;nbsp; The client would like to send the updated Customer entity graph to the mid-tier so that it can be processed and persisted. &lt;/P&gt;
&lt;P&gt;This system will use a simple mechanism to help with change tracking on DTO entities. Each DTO will implement a small interface to help determine the state of the entity using three properties: IsNew, IsModified, and IsDeleted. In this example, original values are not tracked by the client.&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEntityWithChanges&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;{&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsNew { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsModified { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsDeleted { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;}&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;P&gt;When the client makes changes to the Customer entity graph, the only requirement in this scenario is that when an entity changes, the appropriate property will return true to help determine the state of the entity. &lt;/P&gt;
&lt;P&gt;The mid-tier will expose a service method that implements the logic to update the Customer entity graph. Rather than traverse the Customer, her Orders and OrderLines looking for changes, the service method will use a convention based mechanism that knows how to deal with the IEntityWithChanges interfaces. In this scenario, this mechanism takes the form of an extension method to the ObjectContext that will attach an entity graph to the context, and use a delegate to map each entity that implements IEntityWithChanges to an EntityState so that it can be used with the new ChangeObjectState APIs. &lt;/P&gt;
&lt;P&gt;First we can define a method to map from an IEntityWithChanges to an EntityState:&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; A mapping from an IEntityWithChanges to an EntityState&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&lt;SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt; GetEntityState(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; entity)&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;{&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IEntityWithChanges&lt;/SPAN&gt; entityWithChanges = &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; entity &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEntityWithChanges&lt;/SPAN&gt;;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (entityWithChanges != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (entityWithChanges.IsNew)&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;.Added;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (entityWithChanges.IsModified)&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;.Modified;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (entityWithChanges.IsDeleted)&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;.Deleted;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;.Unchanged;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;}&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;P&gt;Next we will define the extension method on an ObjectContext that can attach an entity graph and use a mapping delegate to determine the state of each entity in the graph.&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt; &lt;BR&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; Attach a graph of entities using a supplied mapping function to &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; determine the entity's state. &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; In this example, the state of relationships between entities is &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; determined by the rules of ChangeObjectState: &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; - if one entity in the relationship is Added, the relationship is &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp; Added &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; - if an entity is Deleted or Detached, all incident relationships &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp; are Deleted or Detached &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt; &lt;BR&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="entitySetName"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;name of EntitySet of root&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt; &lt;BR&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="entityGraph"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;graph of entities to attach&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt; &lt;BR&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="entityStateMap"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;EntityState resolver&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&lt;SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Attach( &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this ObjectContext&lt;/SPAN&gt; context, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; entitySetName, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; entityGraph, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;&amp;gt; entityStateMap) &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;B&gt;&amp;nbsp;&lt;/B&gt;context.AttachTo(entitySetName, entityGraph); &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Create a list of unique entities in the graph &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; allEntities = &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ObjectStateManager &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .GetObjectStateEntries(EntityState.Unchanged) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Where(entry =&amp;gt; !entry.IsRelationship) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(entry =&amp;gt; entry.Entity); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Apply the entityStateMap to each entity &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; entity &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; allEntities) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.ChangeObjectState(entity, entityStateMap(entity)); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;}&lt;B&gt;&lt;/B&gt;&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;P&gt;Finally we can define our service method that can update a Customer entity graph using the mapping delegate and the Attach extension method.&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; A service method to update a Customer's entity graph which &lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; can include modifications to the Customer entity, &lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; new Orders, and removal of Orders&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="c"&amp;gt;&amp;lt;/param&amp;gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&lt;SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; UpdateCustomerOrder(&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt; customer)&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;{&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindEntities&lt;/SPAN&gt; context = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NorthwindEntities&lt;/SPAN&gt;())&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Call the Attach extenion method with the mapping delegate &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// that goes from IEntityWithChanges to EntityState&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.Attach(&lt;SPAN style="COLOR: #a31515"&gt;"Customers"&lt;/SPAN&gt;, customer, GetEntityState);&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.SaveChanges();&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;}&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;H3&gt;&lt;SPAN style="FONT-FAMILY: " color:="COLOR:" calibri?,?sans-serif?;="calibri?,?sans-serif?;" #1f497d?="#1f497d?"&gt;&lt;BR&gt;&lt;/SPAN&gt;ChangeOrder Service Operation&lt;/H3&gt;
&lt;P&gt;In this scenario, the mid-tier exposes a method to perform a very specific operation: changing the Customer that owns an Order. The client can set the Order’s Customer to a new Customer or to one that already exists in the store. In either case, the service method uses the old Customer so that it can remove the relationship between the Order and that Customer.&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt; &lt;BR&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; Updates an Order by changing the relationship between the Order &lt;BR&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt; and a Customer in this case &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt; &lt;BR&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="updatedOrder"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;order to update&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt; &lt;BR&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;param name="oldCustomer"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;previous customer&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&lt;SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ChangeOrder(&lt;SPAN style="COLOR: #2b91af"&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Order&lt;/SPAN&gt; updatedOrder, &lt;SPAN style="COLOR: #2b91af"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Customer&lt;/SPAN&gt; oldCustomer) &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindEntities&lt;/SPAN&gt; context = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NorthwindEntities&lt;/SPAN&gt;()) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Tell the context about the entities to persist &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.AttachTo(&lt;SPAN style="COLOR: #a31515"&gt;"Orders"&lt;/SPAN&gt;, updatedOrder); &lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Change the state of the new customer &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// In this example, this is done by the convention: &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// - Customer.CustomerID is null --&amp;gt; customer is 'Added' &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// - Customer.CustomerID is not null --&amp;gt; it already exists &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt;(updatedOrder.Customer.CustomerID == &lt;SPAN style="COLOR: #a31515"&gt;""&lt;/SPAN&gt;) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// When state of customer changes to 'Added', state &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// of the relationship between updatedOrder and &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// pdatedOrder.Customer is also changed to 'Added' &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.ChangeObjectState( &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updatedOrder.Customer, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;.Added); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;else &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (updatedOrder.Customer.CustomerID != &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldCustomer.CustomerID) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// only relationship needs to be marked as 'Added' &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.ChangeRelationshipState( &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updatedOrder, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updatedOrder.Customer, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o =&amp;gt; o.Customer, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;.Added); &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Report removal of relationship between &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // pdatedOrder and oldCustomer&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.ChangeRelationshipState( &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updatedOrder, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; oldCustomer, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Customer", &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;EntityState&lt;/SPAN&gt;.Deleted);&lt;/SPAN&gt; &lt;BR&gt;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Persist the changes to the store &lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; context.SaveChanges(); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;}&lt;/SPAN&gt; &lt;/DIV&gt;
&lt;P&gt;As is always the case, your feedback on this topic is welcome! &lt;/P&gt;
&lt;P&gt;Jeff Derstadt &lt;BR&gt;Jaroslaw Kowalski &lt;BR&gt;Diego Vega &lt;BR&gt;and The Object Services Team&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at &lt;/EM&gt;&lt;/STRONG&gt;&lt;A href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx"&gt;&lt;FONT color=#4c6d7e&gt;&lt;STRONG&gt;&lt;EM&gt;this post&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;STRONG&gt;&lt;EM&gt;.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9127479" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/POCO/default.aspx">POCO</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/ObjectServices/default.aspx">ObjectServices</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/N-Tier/default.aspx">N-Tier</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework+4/default.aspx">Entity Framework 4</category></item><item><title>Foreign Keys in the Conceptual and Object Models</title><link>http://blogs.msdn.com/efdesign/archive/2008/10/27/foreign-keys-in-the-conceptual-and-object-models.aspx</link><pubDate>Mon, 27 Oct 2008 19:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9018535</guid><dc:creator>efdesign</dc:creator><slash:comments>36</slash:comments><comments>http://blogs.msdn.com/efdesign/comments/9018535.aspx</comments><wfw:commentRss>http://blogs.msdn.com/efdesign/commentrss.aspx?PostID=9018535</wfw:commentRss><description>&lt;P&gt;If you are reading this, you have probably heard by now about the so called impedance mismatch between the relational world and the object world – and there are a number of concepts in the relational database that don’t translate easily to corresponding concepts supported by the object oriented paradigm. One of these factors that is particularly interesting (and often controversial) is the concept of Foreign Keys and whether or not they belong in your conceptual/object model.&lt;?xml:namespace prefix = o /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;FKs are used to represent relationships in the database – but with objects, the natural way to represent relationships is through real references between objects. So as an object relational mapping platform, should a product support one or the other, or both?&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;I think most will agree that there is tremendous value in supporting relationships in the model as first class references between objects.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;What are some of the issues with supporting both foreign keys and references in the same model? We could take a real world example i.e. LINQ to SQL and its foreign key support to look at some of the benefits as well as the downsides to having foreign keys.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;H3&gt;Foreign Key Support in LINQ to SQL&lt;o:p&gt;&lt;/o:p&gt;&lt;/H3&gt;
&lt;P&gt;LINQ to SQL takes the simplistic approach of making foreign keys available to you as a scalar property in your entities. In essence, LINQ to SQL allows you to write code dealing with relationships like so:&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN class=type&gt;Product&lt;/SPAN&gt;&amp;nbsp;chai = db.Products.Where(p =&amp;gt; p.ProductName == &lt;SPAN class=stringliteral&gt;"Chai"&lt;/SPAN&gt;).Single(); &lt;BR&gt;chai.CategoryID = 1; &lt;BR&gt;db.SubmitChanges(); &lt;/DIV&gt;
&lt;P&gt;This is possible in LINQ to SQL mainly because foreign keys are somewhat central to the way relationships are implemented and supported. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;This particular example achieves something quite powerful, however - here you have essentially changed the category that the product Chai belonged to without ever having to query and materialize the new category that you associated Chai with.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;However, LINQ to SQL also allows you to do this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;DIV class=codeblock&gt;&lt;SPAN class=type&gt;Product&lt;/SPAN&gt; chai = db.Products.Where(p =&amp;gt; p.ProductName == &lt;SPAN class=stringliteral&gt;"Chai"&lt;/SPAN&gt;).Single(); &lt;BR&gt;&lt;SPAN class=type&gt;Category&lt;/SPAN&gt; beverages = db.Categories.Where(c =&amp;gt; c.CategoryName == &lt;SPAN class=stringliteral&gt;"Beverages"&lt;/SPAN&gt;).Single(); &lt;BR&gt;chai.Category = beverages; &lt;/DIV&gt;
&lt;P&gt;We have been considering for a while about whether or not we want to include support to allow you to expose foreign keys in the model. Let’s see what you gain / lose by having foreign keys in the model.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;One of the fundamental issues is that the simple foreign key concept that works so well in the relational model isn’t sufficient to represent relationships in the conceptual model. In the Entity Framework, a relationship is a first class concept that must be mapped to any set of columns in the database – and the important thing here is that these columns don’t have to represent relationships on the database by the means of FKs and constraints. Another point to note is that in the Entity Framework, relationships are always comprised of two ends, and are bi-directional unlike foreign keys.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;What does all of this do for the model?&amp;nbsp; This opens up possibilities, such as Referential Integrity constraints that are represented in the EDM even though they may not necessarily be present on the relational model in the store. Bi-directional nature of the relationships makes it possible for you to navigate in both directions along a relationship in a way that is generally not possible with a simple FK.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;Foreign Keys are not without value, however. An interesting challenge for us is to figure out how to bring the best aspects of Foreign Keys into the Entity Framework without compromising the richness and flexibility that relationships bring to the table.&amp;nbsp; Let’s look at the upside and the downsides of plain relational foreign keys:&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;H3&gt;Benefits of Foreign Keys&lt;o:p&gt;&lt;/o:p&gt;&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;Keeps it simple (for the simple cases)&amp;nbsp; and allows you to deal with relationship like you deal with them in the database&lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;
&lt;LI&gt;Technically, you can update relationships without having both ends loaded/materialized. This is however in reality not always interesting since you will likely load both ends but this feature is definitely useful.&lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;H3&gt;Disadvantages of Foreign Keys in the Model&lt;o:p&gt;&lt;/o:p&gt;&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;It is a part of the impedance mismatch problem.&lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;
&lt;LI&gt;It doesn’t allow the concepts that you would expect from relationships in objects (easily getting from one end to the other) for instance.&lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;
&lt;LI&gt;Having foreign keys as well as object references for relationship navigation presents the problem of two different artifacts representing relationships – this introduces complexity and now you have to make sure that you keep these two in sync.&lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;So where do you stand? Do you like foreign keys or do you think they are evil? Would you like to see Foreign Keys exposed in the model, and if they are available in your object model, would it be sufficient if they were read-only?&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;Let us know!&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Calibri size=3&gt;&lt;STRONG&gt;Faisal Mohamood&lt;/STRONG&gt;&amp;nbsp; &lt;BR&gt;Program Manager, &lt;BR&gt;Entity Framework Team&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Calibri size=3&gt;This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at &lt;/FONT&gt;&lt;A href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx"&gt;&lt;FONT face=Calibri size=3&gt;this post&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Calibri size=3&gt;.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9018535" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/ObjectServices/default.aspx">ObjectServices</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Linq+to+Sql/default.aspx">Linq to Sql</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework+4/default.aspx">Entity Framework 4</category></item></channel></rss>