<?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 : Self-Tracking Entities</title><link>http://blogs.msdn.com/efdesign/archive/tags/Self-Tracking+Entities/default.aspx</link><description>Tags: Self-Tracking Entities</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Self-Tracking Entities in the Entity Framework</title><link>http://blogs.msdn.com/efdesign/archive/2009/03/24/self-tracking-entities-in-the-entity-framework.aspx</link><pubDate>Tue, 24 Mar 2009 04:50:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9503127</guid><dc:creator>efdesign</dc:creator><slash:comments>67</slash:comments><comments>http://blogs.msdn.com/efdesign/comments/9503127.aspx</comments><wfw:commentRss>http://blogs.msdn.com/efdesign/commentrss.aspx?PostID=9503127</wfw:commentRss><description>&lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 14pt; line-height: 115%; mso-bidi-font-size: 11.0pt"&gt;Background &lt;/span&gt;&lt;/b&gt;    &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;/p&gt; One of the biggest pieces of feedback we received from the &lt;a href="http://blogs.msdn.com/efdesign/archive/2008/11/20/n-tier-improvements-for-entity-framework.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/11/20/n-tier-improvements-for-entity-framework.aspx"&gt;N-Tier Improvements for Entity Framework&lt;/a&gt; post as well as other sources was: &amp;#8220;low level APIs are great, but where is the end-to-end architecture for N-tier and the Entity Framework?&amp;#8221;. &lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;This post outlines some of the additional feedback we&amp;#8217;ve received and describes the self-tracking entities architecture that will ship along side of VisualStudio 2010 and .NET Framework 4.0.  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;When an application goes multi-tier, there are important &lt;a href="http://www.codeplex.com/AppArchGuide" mce_href="http://www.codeplex.com/AppArchGuide"&gt;architectural decisions&lt;/a&gt; that the application developer makes around how to communicate between tiers. There are a lot of choices, and the choice depends on a variety of conditions. How rapidly is each tier expected to change? How much control is there over each tier? Are their specific protocol, security, or business policy concerns? The answers to these questions often drive the selection of whether to use data transfer objects (DTOs) or DataSets or something different altogether.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 14pt; line-height: 115%; mso-bidi-font-size: 11.0pt"&gt;Self-Tracking Entities &lt;/span&gt;&lt;/b&gt;    &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;/p&gt; Self-tracking entities know how to do their own change tracking regardless of which tier those changes are made on. As an architecture, self-tracking entities falls between DTOs and DataSets and includes some of the benefits of each.   &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 12pt; line-height: 115%; mso-bidi-font-size: 11.0pt"&gt;Drawing from DataSet &lt;/span&gt;&lt;/b&gt;    &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;/p&gt; DataSet on the client tier is very easy to use because there is no need to track changes separately or maintain any extra data structures that include change tracking information. DataSet takes care of serializing state information for each row of data. On the mid tier, applying the changes stored within a DataSet is straightforward. DataSets have also gained popularity because of the number of tools that work with DataSets, and because they are easily bound to many UI/presentation controls. Since it works in so many scenarios, for many applications there is never a need to transform data outside of a DataSet allowing a single paradigm to be used up and down the stack.  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;However, there are disadvantages to DataSets when used as the communication payload between tiers. The first is the cost of getting the data into a serializable format, and the second is that the DataSet serialization format is generally not very interoperable with other languages that are used to expose services. Another disadvantage of using a DataSet is that you can quickly lose the intent of your service call because so many kinds of things can be included in a DataSet. For example, if the service method is declared as:&lt;/p&gt;  &lt;p class="MsoNormal" style="background: #dbe5f1; mso-background-themecolor: accent1; mso-background-themetint: 51"&gt;&lt;span style="font-size: 10pt; background: #dbe5f1; line-height: 115%; font-family: " mso-shading-themecolor:="mso-shading-themecolor:" accent1;="accent1;" mso-shading-themetint:="mso-shading-themetint:" 51?="51?" courier="courier" new?;="new?;"&gt;DataSet GetCustomer(string id); &lt;/span&gt;&lt;/p&gt;  &lt;p mce_keep="true"&gt;&lt;/p&gt; It is extra work to ensure that the DataSet that is returned contains only rows of data that pertain to &amp;#8220;customers&amp;#8221;. It is also more difficult to specify a service contract that says the DataSet is also supposed to return data for each customer&amp;#8217;s orders and their order details.  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;Self-tracking entities share many advantages with DataSet. They also encapsulate change tracking information, which is serialized along with the data contained in the entity. On the mid-tier applying changes from a graph of self-tracking entities to a persistent context is equally &lt;a href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=10661&amp;amp;postid=9503077#Midtier" mce_href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=10661&amp;amp;postid=9503077#Midtier"&gt;straightforward&lt;/a&gt;. The Entity Framework will also provide tools for &lt;a href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=10661&amp;amp;postid=9503077#Generating" mce_href="http://blogs.msdn.com/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=10661&amp;amp;postid=9503077#Generating"&gt;generating self-tracking entities&lt;/a&gt; and because these entities are just objects, they can be easily made to work with UI/presentation controls.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 12pt; line-height: 115%; mso-bidi-font-size: 11.0pt"&gt;Drawing from DTOs &lt;/span&gt;&lt;/b&gt;    &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;/p&gt; DTOs and SOA are used to give the developer more control over the service contract and payload for tier to tier communication. DTOs themselves do not have behavior, so they are typically very simple classes designed just to provide the needed information to perform a specific service operation. Not only does this provide the opportunity to optimize the wire format (some believe a DTO is all about the wire format), but it makes it possible and easy to capture the intent of each service method. The data contract used with DTOs is typically interoperable which makes it easy to use services that run on different platforms. DTOs also provide a way to separate messaging contracts from the presentation layer, the business logic, and the persistence layer which in many cases creates a maintainable architecture.   &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;There are disadvantages to using DTOs and the primary one is complexity. DTOs are often hand-crafted to include only the specific information that is needed for an operation. When there is a common pattern for mapping DTOs to entity classes, there are some tools available that will do DTO generation and mapping but it is not always possible. With DTOs, it is up to the developer to decide how to do change tracking on each tier (especially the client) which increases the complexity of the presentation layer. The complexity of the service implementation also increases because the developer is responsible for translating the DTO into entities for doing business logic validation, as well as being able to report changes stored in the DTO to the persistence framework. &lt;/p&gt;  &lt;p class="MsoNormal"&gt;Not only do self-tracking entities share advantages with DataSets as mentioned above, they also share many advantages with DTOs. Self-tracking entities expose a simple and interoperable wire format and it is clear what kinds of data a service method requires or returns. You can also use a self-tracking entity as part of a message. However, self-tracking entities don&amp;#8217;t give quite as much architectural separation as using pure DTOs, but you do gain a less complex solution that requires fewer data transformations. It is important to note that self-tracking entities can be made to be ignorant of any particular persistence framework making them essentially POCO objects. Particular persistence frameworks such as the Entity Framework will have the capabilities to create these entities and interpret the change tracking information when saving changes.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 12pt; line-height: 115%; mso-bidi-font-size: 11.0pt"&gt;.NET Framework 3.5SP1 Challenge &lt;/span&gt;&lt;/b&gt;    &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;/p&gt; With the .NET Framework 3.5SP1, this sort of solution was very hard to implement using the Entity Framework because change tracking was always done by a centralized ObjectContext which contains an ObjectStateManager. In particular, &amp;#8220;reattaching&amp;#8221; to report changes back to the ObjectStateManager was all but impossible without completely shredding your entity graph and applying changes one entity and one relationship at a time. The &lt;a href="http://blogs.msdn.com/efdesign/archive/2008/11/20/n-tier-improvements-for-entity-framework.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/11/20/n-tier-improvements-for-entity-framework.aspx"&gt;new API changes&lt;/a&gt; that are being added to the Entity Framework in .NET Framework 4.0 are enablers for an easier experience of reporting changes back to the ObjectStateManager, making self-tracking entities (as well as other architectures) easier to build.  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;a name="Midtier"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 14pt; line-height: 115%; mso-bidi-font-size: 11.0pt"&gt;Mid-tier experience &lt;/span&gt;&lt;/b&gt;      &lt;p mce_keep="true"&gt;&lt;/p&gt;   &lt;/a&gt;&lt;/p&gt; &lt;span style="mso-bookmark: midtier"&gt;Using self-tracking entities on the mid-tier is about working with entity graphs and the Entity Framework. The service contract that is used in the following examples contains two simple methods for retrieving a Customer entity graph and applying updates to that entity graph:&lt;/span&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="background: #dbe5f1; margin-bottom: 0pt; line-height: normal; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-layout-grid-align: none"&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;interface &lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;ICustomerService        &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;{        &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; color: #2b91af; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Customer&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt; GetCustomer(&lt;span style="color: blue"&gt;string&lt;/span&gt; customerID);         &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;bool &lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;UpdateCustomer(&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt; customer);         &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: midtier"&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;} &lt;/span&gt;&lt;/span&gt;    &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;/p&gt; &lt;span style="mso-bookmark: midtier"&gt;The implementation of the GetCustomer service method can be done using an Entity Framework ObjectContext and using LINQ or query builder methods to retrieve the entity you want. In the example below, a query is issued for a particular customer entity and all of the Orders and OrderDetails are included.&lt;/span&gt;  &lt;p&gt;&lt;/p&gt; &lt;span style="mso-bookmark: midtier"&gt;&lt;/span&gt;  &lt;p class="MsoNormal" style="background: #dbe5f1; margin-bottom: 0pt; line-height: normal; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;public&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt; &lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt; GetCustomer(&lt;span style="color: blue"&gt;string&lt;/span&gt; customerID)       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;{      &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;NorthwindEFContext&lt;/span&gt; context = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NorthwindEFContext&lt;/span&gt;())       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; result = context.Customers.       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Include(&lt;span style="color: #a31515"&gt;&amp;quot;Orders.OrderDetails&amp;quot;&lt;/span&gt;).       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Single(c =&amp;gt; c.CustomerID == customerID);       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt; result;       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;} &lt;/span&gt;&lt;/p&gt;  &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;span style="mso-no-proof: yes"&gt;UpdateCustomer is example of how to save the changes that are made to a graph of self-tracking entities. Similar to DataSet, applying these changes to the persistence layer and saving them should be simple. In the below example, a new Entity Framework API, &amp;#8220;ApplyChanges&amp;#8221; is used which understands how to interpret the change tracking information that is stored by each entity and how to tell the ObjectContext&amp;#8217;s ObjectStateManager about those changes. &lt;/span&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="background: #dbe5f1; margin-bottom: 0pt; line-height: normal; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;public&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt; &lt;span style="color: blue"&gt;void &lt;/span&gt;UpdateCustomer(&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt; customer)       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;{      &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;NorthwindEFContext&lt;/span&gt; context = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NorthwindEFContext&lt;/span&gt;())       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;context.Customers.ApplyChanges(customer);       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;context.SaveChanges();       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;}&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 14pt; line-height: 115%; mso-bidi-font-size: 11.0pt"&gt;Client Experience &lt;/span&gt;&lt;/b&gt;    &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;/p&gt; The client experience when working with self-tracking entities is similar to how you would manipulate any object graph. You can make changes to scalar or complex properties, add or remove references, and add or remove from collections of related entities. The key part of the experience is that tracking changes is hidden from the client because it is done internally on each entity. There is no ObjectContext and there is no extra state that has to be maintained or passed from client to the tier that does persistence.   &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;In this example, the test first queries for a customer, then deletes one of the orders. The resulting entity graph is sent back to the service tier using &lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;the UpdateCustomer method.&lt;/p&gt;  &lt;p class="MsoNormal" style="background: #dbe5f1; margin-bottom: 0pt; line-height: normal; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;public&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DeleteObjectsTest()       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;{      &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CustomerServiceClient&lt;/span&gt; client = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CustomerServiceClient&lt;/span&gt;())       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; customer = client.GetCustomer(&lt;span style="color: #a31515"&gt;&amp;quot;ALFKI&amp;quot;&lt;/span&gt;);       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;customer.Orders.First().Delete();       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;client.UpdateCustomer(customer);       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;} &lt;/span&gt;&lt;/p&gt;  &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;span style="mso-no-proof: yes"&gt;The next test shows how to add a new Order with two OrderDetails to an existing customer. By default, the constructor of a self-tracking entity puts the entity in the &amp;#8220;Added&amp;#8221; state. There are conveience methods on each self-tracking entity to change this state if needed (Delete() and SetUnchanged()). &lt;/span&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="background: #dbe5f1; margin-bottom: 0pt; line-height: normal; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;public&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddObjectsTest()       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;{      &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CustomerServiceClient&lt;/span&gt; client = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CustomerServiceClient&lt;/span&gt;())       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; customer = client.GetCustomer(&lt;span style="color: #a31515"&gt;&amp;quot;ALFKI&amp;quot;&lt;/span&gt;);       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;customer.Orders.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Order&lt;/span&gt;() {&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OrderID = 100,       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;OrderDetails = {       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OrderDetail&lt;/span&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ProductID = 3,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Quantity = 7       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; },       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OrderDetail&lt;/span&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ProductID = 4,       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Quantity = 8       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; },       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;client.UpdateCustomer(customer);       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;} &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;The final example shows that a self-tracking entity graph&lt;span style="mso-spacerun: yes"&gt;&amp;#160; &lt;/span&gt;can contain any number of changes and those changes can be any combination of adds, modifications, and deletes. &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="background: #dbe5f1; margin-bottom: 0pt; line-height: normal; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;public&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ComplexModificationsTest ()       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;{      &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CustomerServiceClient&lt;/span&gt; client = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CustomerServiceClient&lt;/span&gt;())       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; cust = client.GetCustomer(&lt;span style="color: #a31515"&gt;&amp;quot;ALFKI&amp;quot;&lt;/span&gt;);       &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: green"&gt;// remove first order - will do cascade in the database        &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;cust.Orders.Remove(cust.Orders.First());       &lt;br /&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: green"&gt;// modify one of the orders          &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; order = cust.Orders.Last();         &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;order.RequiredDate = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;         &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;order.OrderDetails.Add(         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OrderDetail&lt;/span&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ProductID = 7,&amp;#160; &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;Quantity = 3&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// add new order          &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;cust.Orders.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Order&lt;/span&gt;()           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;{           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;OrderID = 100,           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;OrderDate = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now,           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;ShipCity = &lt;span style="color: #a31515"&gt;&amp;quot;Redmond&amp;quot;&lt;/span&gt;,           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;ShipAddress = &lt;span style="color: #a31515"&gt;&amp;quot;One Microsoft Way&amp;quot;&lt;/span&gt;,           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;ShipRegion = &lt;span style="color: #a31515"&gt;&amp;quot;WA&amp;quot;&lt;/span&gt;,           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;ShipPostalCode = &lt;span style="color: #a31515"&gt;&amp;quot;98052&amp;quot;&lt;/span&gt;,           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;OrderDetails = {           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OrderDetail&lt;/span&gt;{           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ProductID = 3,           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Quantity = 7           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; },           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OrderDetail&lt;/span&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ProductID = 4,           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Quantity = 8           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; },           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;});           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;client.UpdateCustomer(cust);           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;}           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;} &lt;/span&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " courier="courier" new?;="new?;" mso-no-proof:="mso-no-proof:" yes?="yes?"&gt;&lt;/span&gt;&lt;/span&gt;    &lt;p&gt;&lt;/p&gt; &lt;/p&gt; &lt;a name="Generating"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 14pt; line-height: 115%; mso-bidi-font-size: 11.0pt; mso-no-proof: yes"&gt;Generating Self-Tracking Entities &lt;/span&gt;      &lt;p mce_keep="true"&gt;&lt;/p&gt;   &lt;/b&gt;&lt;/a&gt;&lt;span style="mso-no-proof: yes"&gt;Building self-tracking entities should be as easy as using them. We plan to ship a &lt;/span&gt;&lt;a href="http://blogs.msdn.com/efdesign/archive/2009/01/22/customizing-entity-classes-with-t4.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2009/01/22/customizing-entity-classes-with-t4.aspx"&gt;&lt;span style="mso-no-proof: yes"&gt;T4 template&lt;/span&gt;&lt;/a&gt;&lt;span style="mso-no-proof: yes"&gt; that will do the code generation for creating self-tracking entities from your EDM. This template will show up in the list of available templates when you right click on your model and choose &lt;i style="mso-bidi-font-style: normal"&gt;Add New Artifact Generation Item&lt;/i&gt;. &lt;/span&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/SelfTrackingEntitiesintheEntityFramework_10506/clip_image002_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/SelfTrackingEntitiesintheEntityFramework_10506/clip_image002_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="149" alt="clip_image002" src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/SelfTrackingEntitiesintheEntityFramework_10506/clip_image002_thumb.jpg" width="244" border="0" v:shapes="Picture_x0020_1" mce_src="http://blogs.msdn.com/blogfiles/efdesign/WindowsLiveWriter/SelfTrackingEntitiesintheEntityFramework_10506/clip_image002_thumb.jpg" /&gt;&lt;/a&gt;&amp;#160;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 14pt; line-height: 115%; mso-bidi-font-size: 11.0pt; mso-no-proof: yes"&gt;Design Notes &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 12pt; line-height: 115%; mso-bidi-font-size: 11.0pt; mso-no-proof: yes"&gt;Inside a Self-Tracking Entity &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;There have not been final decisions about what exactly will be included inside of a self-tracking entity, but it is important to track:&lt;/span&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;The state of the entity, Added, Deleted, Modified, or Unchanged&lt;/span&gt;&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;The original value from reference relationship properties&lt;/span&gt;&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;Adds and removes from collection relationship properties &lt;/span&gt;&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;This information will be included with the data contract of the entity. As part of the code generation of each self-tracking entity, scalar and complex property changes will mark the entity as &amp;#8220;dirty&amp;#8221; meaning that its state will change from Unchanged to Modified. &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 12pt; line-height: 115%; mso-bidi-font-size: 11.0pt; mso-no-proof: yes"&gt;Inside ApplyChanges &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;ApplyChanges is a new API on the ObjectContext and ObjectSet classes that attaches an entity graph and interprets the change tracking information stored in each entity. The design &lt;span style="mso-spacerun: yes"&gt;&amp;#160;&lt;/span&gt;for discovering the change tracking information has not yet been finalized, but there are a couple of options available. One would be to code generate an interface as part of the T4 template for self-tracking entities as well as a version of ApplyChanges that knew about the interface. Another option would be fall back to runtime discovery of the particular properties using a convension and some of the new dynamic capabilities of the framework. The thing we want to avoid is causing the self-tracking entity implementation to have a dependency on any of the Entity Framework assemblies. &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;The algorithm that ApplyChanges uses is:&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;1.&lt;span style="font-weight: normal; font-size: 7pt; line-height: normal; font-style: normal; font-variant: normal" times="times" roman??="roman??" new="new"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;Attaching the entity graph to the ObjectContext&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;2.&lt;span style="font-weight: normal; font-size: 7pt; line-height: normal; font-style: normal; font-variant: normal" times="times" roman??="roman??" new="new"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;Changing the state of each entity using the ChangeObjectState API.&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;3.&lt;span style="font-weight: normal; font-size: 7pt; line-height: normal; font-style: normal; font-variant: normal" times="times" roman??="roman??" new="new"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;For any reference relationship property, if there is an original value changing the state of the original reference relationship to Deleted and change the state of the current reference relationship to Added. This is done using the ChangeRelationshipState API.&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;span style="mso-list: ignore"&gt;4.&lt;span style="font-weight: normal; font-size: 7pt; line-height: normal; font-style: normal; font-variant: normal" times="times" roman??="roman??" new="new"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;For any collection relationship property, use the ChangeRelationshipState API to mark any removed relationships as Deleted and to mark any added relationships as Added. &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="font-size: 14pt; line-height: 115%; mso-bidi-font-size: 11.0pt; mso-no-proof: yes"&gt;Summary &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="mso-no-proof: yes"&gt;We&amp;#8217;ve received a lot of feedback and suggestions on how to make developing multi-tier applications easier using the Entity Framework. One of the components of improving this experience is the introduction of an end-to-end architecture around self-tracking entities. We&amp;#8217;d like to hear your feedback on this addition to the Entity Framework, and any other comments you have on the matter of multi-tier development using domain models and entities. &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"&gt;&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="mso-no-proof: yes"&gt;Jeff Derstadt&lt;/span&gt;&lt;/b&gt;&lt;span style="mso-no-proof: yes"&gt;,      &lt;br /&gt;&lt;/span&gt;&lt;span style="mso-no-proof: yes"&gt;Dev Lead, Entity Framework Team, Microsoft &lt;/span&gt;&lt;/p&gt;  &lt;p mce_keep="true"&gt;&lt;/p&gt; &lt;em&gt;&lt;b&gt;&lt;span style="font-size: 11pt; font-family: " mso-ascii-theme-font:="mso-ascii-theme-font:" minor-latin;="minor-latin;" mso-hansi-theme-font:="mso-hansi-theme-font:" minor-latin?="minor-latin?" calibri?,?sans-serif?;="calibri?,?sans-serif?;"&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;/span&gt;&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;&lt;span style="font-size: 11pt; color: blue; font-family: " mso-ascii-theme-font:="mso-ascii-theme-font:" minor-latin;="minor-latin;" mso-hansi-theme-font:="mso-hansi-theme-font:" calibri?,?sans-serif?;="calibri?,?sans-serif?;" TEXT-DECORATION:="TEXT-DECORATION:" none;="none;" text-underline:="text-underline:" none?="none?"&gt;this post&lt;/span&gt;&lt;/b&gt;&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&lt;b&gt;&lt;span style="font-size: 11pt; font-family: " mso-ascii-theme-font:="mso-ascii-theme-font:" minor-latin;="minor-latin;" mso-hansi-theme-font:="mso-hansi-theme-font:" minor-latin?="minor-latin?" calibri?,?sans-serif?;="calibri?,?sans-serif?;"&gt;. &lt;/span&gt;&lt;/b&gt;&lt;/em&gt;&lt;span style="font-size: 11pt; font-family: " mso-ascii-theme-font:="mso-ascii-theme-font:" minor-latin;="minor-latin;" mso-hansi-theme-font:="mso-hansi-theme-font:" minor-latin?="minor-latin?" calibri?,?sans-serif?;="calibri?,?sans-serif?;"&gt;&lt;/span&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9503127" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/efdesign/archive/tags/POCO/default.aspx">POCO</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Self-Tracking+Entities/default.aspx">Self-Tracking Entities</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/T4/default.aspx">T4</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Templates/default.aspx">Templates</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework+4/default.aspx">Entity Framework 4</category></item></channel></rss>