{"id":1393,"date":"2008-09-04T05:02:00","date_gmt":"2008-09-04T05:02:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/cesardelatorre\/2008\/09\/04\/updating-data-using-entity-framework-in-n-tier-and-n-layer-applications-short-lived-ef-contexts-part-1\/"},"modified":"2008-09-04T05:02:00","modified_gmt":"2008-09-04T05:02:00","slug":"updating-data-using-entity-framework-in-n-tier-and-n-layer-applications-short-lived-ef-contexts-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/updating-data-using-entity-framework-in-n-tier-and-n-layer-applications-short-lived-ef-contexts-part-1\/","title":{"rendered":"Updating data using Entity Framework in N-Tier and N-Layer Applications (short lived EF contexts) &#8211; (Part 1)"},"content":{"rendered":"<p><P>First of all, we are talking about using <B>Entity Framework<\/B> and how it fits within <B>N-Tier<\/B> and <B>N-Layer<\/B> applications, ok?, that\u2019s our initial scenario.<\/P>\n<P>An <B>N-Tier application<\/B> is an application where you have 3 or more <U>physical tiers<\/U>. I mean with that things like, \u201cPresentation\/Client Tier\u201d, \u201cApplication\/Business Server Tier\u201d and \u201cData Tier\u201d (a database server in most of the cases) and nowadays we use web services (or even better, WCF Services) to communicate between presentation tier and the application server tier. Here you see a simple &amp; typical picture about <B>N-Tier architecture<\/B>:<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/clip_image002_2.jpg\"><IMG height=\"264\" alt=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/clip_image002_thumb.jpg\" width=\"581\" border=\"0\"><\/A><\/P>\n<P>As you can see, all the client applications (we could be for instance <B>WinForms<\/B>, <B>WPF<\/B>, or even <B>Silverlight<\/B> apps) need to remotely access to the application Server tier using for example <B>WCF Services<\/B>.<\/P>\n<P>Also, in my opinion. <STRONG>RIA<\/STRONG> (<EM><STRONG>Rich Internet Applications<\/STRONG><\/EM>) is a special subset of N-Tier apps.<\/P>\n<P>A different matter is that we\u2019d probably design our application as an <B><I>N-Layer<\/I><\/B> application, I mean, with several logic layers where we implement different logic tasks. For example we could have the <B>DAL layer<\/B> (<I>Data Access Layer<\/I>), <B>BLL Layer<\/B> (<I>Business Logic Layer<\/I>), <B>BFLL Layer<\/B> (Busines Fa\u00e7ade Logic Layer), <B>WCF Service Layer<\/B> and several <B>Presentation layers<\/B> depending of the pattern we use, like <B>MVC <\/B>(Model-View-Controller), <B>MVP <\/B>(Model-View-Presenter), etc.. Also, within the <B>N-Layer architecture<\/B>, you can guess that <B>Entity Framework<\/B> fits as the <B>DAL Layer<\/B> (<I>Data Access Classes<\/I>) as well as using EF entities as our disconnected entities to pass thru all the layers, all right?<\/P>\n<P><A href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/image_4.png\"><IMG height=\"428\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/image_thumb_1.png\" width=\"582\" border=\"0\"><\/A> <\/P>\n<P>BTW, not all N-Layer apps should be N-Tier apps, but all N-Tier Apps must internally be designed as N-Layer. I mean, there are many cases where the less you physically split your model, the better for performance (more tiers is good for scalability but not for pure performance, due to latencies). Remember, N-Layer is about logic layers.<\/P>\n<P>OK!, so if we get back to the <B>N-Tier architecture<\/B> (<B>physical tiers<\/B>), like I said, we need remote mechanisms to communicate the client tier with the application server tier (for instance, WCF Services) and therefore when we query the database from the app server tier, to obtain data (like an Order), we keep it as an EF entity, then we disconnect it from the EF context (detach), WCF serializes it and sends that disconnected entity to the presentation tier (client apps &amp; machines). <\/P>\n<P>So, most enterprise applications use an architectural pattern that needs a stateless facade for its business logic (like N-Tier &amp; N-Layer apps). They have scenarios like WebServices or WCF, ASP.NET applications, Traditional Desktop Application (Windows Forms, WPF) consuming WCF Services, etc. in those cases data access works \u201cdisconnected oriented\u201d, I mean, database data (in the real tables) are not blocked while the user is working in the client tier (btw, this is better for scalability). So, in the client\/presentation tier, the user will be working and maybe, he changes that entity\u2019s data (which is disconnected from the server) and after a while (at any time) the user submits that data to the application server and then the DAL layer (Entity Framework and LINQ to EF, in our case) will update that data into the database. Right?. Well, if we just do that, it will be just a <B>\u201cLast-In-Wins\u201d<\/B> or what I call, a <B>\u201cToo Optimistic Update\u201d<\/B> \ud83d\ude09 . Well, in many apps which are not very exigent or complex regarding updates, this can be enough, but then, we could have other apps where the users need to know if data (regarding the same entity and records he is using at client tier) has changed in the database while he was working and before he tried to update the data. In that case we should use \u201cOptimistic Concurrency Updates\u201d managing \u201cOptimistic Concurrency Exceptions\u201d.<\/P>\n<P>So, I am using all the following stuff:<\/P>\n<P>&#8211; <B>Entity Framework<\/B> and <B>LINQ to Entities<\/B> as our <B>DAL Layer<\/B><\/P>\n<P>&#8211; <B>WCF<\/B> as our remote communication technology<\/P>\n<P>&#8211; Any .NET UI technology as our client App (WPF, WinForms, OBA or even Silverlight) <\/P>\n<H4>Simple way: Updating data in N-Tier applications and using Entity Framework with detached entities<\/H4>\n<P>In order to understand what I mean, first of all, I am going to explain how to implement just simple updates in N-Tier applications and using Entity Framework. So this case is the one I called \u201cLast-In-Wins\u201d or a \u201cToo Optimistic Update\u201d ;-). So in this first case I won\u2019t manage any Optimistic Concurrency Exception.<\/P>\n<P>First of all, we\u2019d have a business class (BLL class) which would be using EF &amp; LINQ to Entities so that class would be querying the database; let\u2019s say to get a Customer data and to return that entity\u2019s data to the client\/presentation tier (thru a WCF Service). The BLL class code could be something like the following code:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"648\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"644\">\n<P>public class CustomerBll<\/P>\n<P>{<\/P>\n<P>&nbsp;&nbsp;&nbsp; private MyDataBaseEntities context;<\/P>\n<P>&nbsp;&nbsp;&nbsp; public CustomerBll()<\/P>\n<P>&nbsp;&nbsp;&nbsp; {<\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context = new MyDataBaseEntities();<\/P>\n<P>&nbsp;&nbsp;&nbsp; }<\/P>\n<P>&nbsp;&nbsp;&nbsp; public Customer GetCustomer(string customerID)<\/P>\n<P>&nbsp;&nbsp;&nbsp; {<\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var q = from c in context.Customers \/\/.Include(&#8220;Orders&#8221;)<\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where c.CustomerID == numCustomerID<\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select c;<\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var customer = q.First();<\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.Detach(customer);<\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return customer;<\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Ok, if you know LINQ to entities, this is a very simple method, right?. The important thing to take into account is the following: \u201c<B>we are returning a disconnected\/detached entity data object<\/B>\u201d passing it to the upper layers (Service layer\u00e0Presentation Layers). It means that regarding Entity Framework we\u2019ll be working with <I>ObjectContext<\/I> objects in a \u201c<B>short lived context<\/B>\u201d way. <U>Basically, when we\u2019ll close the loop, we\u2019ll need to re-attach modified EntityObjects, that were updated outside of an ObjectContext (in detached state)<\/U>. This is a very common scenario when we&#8217;re using stateless facades (most N-Tier &amp; N-Layer apps).<\/P>\n<P>Coming back to our code, then we could have a WCF Service serializing &amp; returning the disconnected\/detached entity.<\/P>\n<P>Here you could see the Service contract for the method:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"651\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"647\">\n<P>\/\/WCF Contract<\/P>\n<P>[OperationContract] <\/P>\n<P>Customer CustomerBll_GetCustomer(string customerID); <\/P>\n<P>&#8230;<\/P>\n<P>&#8230;<\/P>\n<P>\/\/And now, the method\u2019s implementation in the WCF class library: <\/P>\n<P>public Customer CustomerBll_GetCustomer(string customerID) <\/P>\n<P>{ <\/P>\n<P>&nbsp;&nbsp;&nbsp; return new CustomerBll().GetCustomer(customerID); <\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Ok, after that, the client app would be consuming the WCF Service and will display the entity\u2019s data (customer\u2019s data) let\u2019s say in a WPF form. Remember that right now, the client app is working with a disconnected\/detached entity object and there is not an ObjectStateManager tracking changes. We\u2019ll see how it affects later on\u2026<\/P>\n<P>So, once the data is visible within a form, the user can start changing some data in that form until he decides to actually update that data into the application (for me, it means he wants to update that data into the database), pressing, for instance a big button which says \u201cUpdate\u201d. J <BR>Right, in this very moment the client application would be calling another WCF Service method called something like UpdateCustomer(), like you see down below:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"652\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"648\">\n<P>public void CustomerBll_UpdateCustomer(Customer customer) <\/P>\n<P>{ <\/P>\n<P>&nbsp;&nbsp;&nbsp; new CustomerBll().UpdateCustomer(customer); <\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Then, that WCF method is calling the real business logic method, and this is where I am going to show you how to use Entity Framework to update the detached\/disconnected but modified entity which is coming back from the client tier. This is the code (remember this is the simple case, I am not managing optimistic concurrency exceptions here):<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"650\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"646\">\n<P>public void UpdateCustomer(Customer customer) <\/P>\n<P>{ <\/P>\n<P>&nbsp;&nbsp;&nbsp; \/\/(CDLTLL) Entity must first be reunited with a Context <\/P>\n<P>&nbsp;&nbsp;&nbsp; \/\/Note I do it with my custom extensor method for disconected environments (N-Tier, etc.) <\/P>\n<P>&nbsp;&nbsp;&nbsp; context.<B>AttachUpdated(customer)<\/B>; \/\/Custom extensor method <\/P>\n<P>&nbsp;&nbsp;&nbsp; context.<B>SaveChanges();<\/B> <\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Ok, first, in order for the context to be able to update anything (the entity) into the database using SaveChanges(), it has first to have it attached to the context. So, context.SaveChanges() is an EF regular method we always gotta use to update data (changes). But if we\u2019d just use the method context.Attach(customer) before that, it won\u2019t work because that customer entity is new for the context (it was originally detached, serialized and sent to the client tier) and now the context does not know nothing about it, the context \u201cthinks\u201d it is a new entity object and it would not know any changes to update using the context.SaveChanges().<\/P>\n<P>The following is wrong code:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"648\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"644\">\n<P>public void UpdateCustomer(Customer customer) \/\/WRONG CODE <\/P>\n<P>{ <\/P>\n<P>&nbsp;&nbsp;&nbsp; \/\/(CDLTLL) Entity must first be reunited with a Context <\/P>\n<P>&nbsp;&nbsp;&nbsp; context.<B>Attach (customer)<\/B>; \/\/Custom extensor method <\/P>\n<P>&nbsp;&nbsp;&nbsp; context.<B>SaveChanges();<\/B>\/\/In this case, nothing happens here\u2026 <\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>So in this simpler case, when you\u2019d call SaveChanges() nothing would happen.<\/P>\n<P>That is why I created a custom EF extensor method called \u201ccontext.AttachUpdated()\u201d. To be able to attach updated entities which are disconnected and coming from the client Tier.<\/P>\n<P>It is the following code:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"920\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"916\">\n<P>public static void AttachUpdated(this ObjectContext context, EntityObject objectDetached) <\/P>\n<P>{ <\/P>\n<P>&nbsp;&nbsp;&nbsp; if (objectDetached.EntityState == EntityState.Detached) <\/P>\n<P>&nbsp;&nbsp;&nbsp; { <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object currentEntityInDb = null; <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (<B>context.TryGetObjectByKey<\/B>(objectDetached.EntityKey, out currentEntityInDb)) <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <\/P>\n<P><B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.ApplyPropertyChanges<\/B>(objectDetached.EntityKey.EntitySetName, objectDetached); <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/(CDLTLL)Apply property changes to all referenced entities in context <\/P>\n<P><B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.ApplyReferencePropertyChanges<\/B>((IEntityWithRelationships)objectDetached, <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (IEntityWithRelationships)currentEntityInDb); \/\/Custom extensor method <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new ObjectNotFoundException(); <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <\/P>\n<P>&nbsp;&nbsp;&nbsp; } <\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>So, first of all we need to know what is new in this entity, I mean, what are the changes compared with the data we actually have in the database. In order to do that we\u2019d need to query the database and get the actual data using the \u201ccontext.TryGetObjectByKey()\u201d method and attach it as the original data for that entity within the context.<\/P>\n<P>Then, we can call the \u201ccontext.ApplyPropertyChanges()\u201d providing our new updated entity. What EF does is that it compares the original data (actual data in the database) with our new\/updated entity and then it updates within the context all the property changes just regarding our specific entity (Customer, in this case). After that, when we call to \u201ccontext.<B>SaveChanges()<\/B>\u201d in the main method, EF will detect what are actually those changes and it will be able to update it into the real database (SQL Server, for instance).<\/P>\n<P>Notice that after calling to \u201ccontext.ApplyPropertyChanges()\u201d I am calling another method called \u201cApplyReferencePropertyChanges()\u201d. This other method is actually another extensor method which applies all my entity property changes but into all the referenced entities within our EF model (Customer could be related to Company, Order, etc.). This is an important method, otherwise we\u2019ll be taking into account just our isolated entity (Customer). Here you can see my \u201c<B>context.ApplyReferencePropertyChanges()<\/B>\u201d custom context extensor method:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"923\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"919\">\n<P>public static void ApplyReferencePropertyChanges(this ObjectContext context, <\/P>\n<P>IEntityWithRelationships newEntity, <\/P>\n<P>IEntityWithRelationships oldEntity) <\/P>\n<P>{ <\/P>\n<P>&nbsp;&nbsp;&nbsp; foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds()) <\/P>\n<P>&nbsp;&nbsp;&nbsp; { <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var oldRef = relatedEnd as EntityReference; <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (oldRef != null) <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ this related end is a reference not a collection <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference; <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; oldRef.EntityKey = newRef.EntityKey; <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <\/P>\n<P>&nbsp;&nbsp;&nbsp; }&nbsp; <\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>So with that, we\u2019re done!, it works ok following the method I said \u201cLast In Wins\u201d or a \u201cToo Optimistic Update\u201d \ud83d\ude09<\/P>\n<P>Btw, <U>another option<\/U> (it works almost the same) would be not to query the database to know the actual data (kind of original data for the context) with \u201ccontext.TryGetObjectByKey()\u201d but to say to the context that <I>all the properties within my entity have actually changed<\/I>, \u201c<I>just because I say it<\/I>\u201d. After that, when we call \u201ccontext.<B>SaveChanges()<\/B>\u201d it will update all the entity properties just because we said that!. J<\/P>\n<P>Here you can see this other entity extensor method (In this case I coded as an entity extensor method instead as a context extensor method, ok?):<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"927\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"923\">\n<P>public static void SetAllModified&lt;T&gt;(this T entity, ObjectContext context) where T : IEntityWithKey <\/P>\n<P>{ <\/P>\n<P>&nbsp;&nbsp;&nbsp; var stateEntry = context.ObjectStateManager.GetObjectStateEntry(entity.EntityKey); <\/P>\n<P>&nbsp;&nbsp;&nbsp; var propertyNameList = stateEntry.CurrentValues.DataRecordInfo.FieldMetadata.Select(pn =&gt; pn.FieldType.Name); <\/P>\n<P>&nbsp;&nbsp;&nbsp; foreach (var propName in propertyNameList) <\/P>\n<P>&nbsp;&nbsp;&nbsp; { <\/P>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stateEntry.SetModifiedProperty(propName); <\/P>\n<P>&nbsp;&nbsp;&nbsp; } <\/P>\n<P>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>In this other case (using SetAllModified()) our code within &#8220;UpdateCustomer()&#8221; would be somethin like:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"2\" width=\"898\" border=\"2\">\n<TBODY>\n<TR>\n<TD class=\"\" vAlign=\"top\" width=\"894\">\n<P>public void UpdateCustomer(Customer customer) <BR>{ <BR>&nbsp;&nbsp;&nbsp; <STRONG>context.Attach<\/STRONG>(customer); <BR>&nbsp;&nbsp;&nbsp; <STRONG>customer.SetAllModified<\/STRONG>(context);&nbsp;&nbsp;&nbsp;&nbsp; \/\/ custom extension method <BR>&nbsp;&nbsp;&nbsp; <STRONG>context.SaveChanges<\/STRONG>(); <BR>}<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Cool!, this seems all great, we are happy and we are living in a wonderful world. Sure?. Well, it depends, but like I said at the beginning of this post, this code we have written does not cover scenarios where we want to manage \u201cOptimistic Concurrency Updates &amp; Exceptions\u201d, I mean, let\u2019s say any other guy changed some data, regarding that same data customer, during the timeframe after I performed my Customer query but before I actually updated it to the database. I mean, someone changed data in the database just before I called to <STRONG>context.AttachUpdated(customer)<\/STRONG> and context.<B>SaveChanges(). <\/B>Ok, then, the initial original user won\u2019t be aware that he might be over writing that new data that the other second user updated\u2026 In many cases this can be dangerous depending on the application\u2019s type.<\/P>\n<P>Well, I guess this post is getting too long, I\u2019ll write a second post (very soon) explaining <EM>\u201cHow to: Update data in N-Tier applications and using <B>Entity Framework<\/B> with detached entities and <B>managing Optimistic Concurrency Updates and Exceptions<\/B>\u201d.<\/EM><\/P>\n<P><EM>&#8212; My second part of this post-article is here:<\/EM><\/P>\n<P><STRONG><U>Optimistic Concurrency Updates using Entity Framework in N-Tier and N-Layer Applications (Part 2)<\/U><\/STRONG><\/P>\n<P><A title=\"http:\/\/blogs.msdn.com\/cesardelatorre\/archive\/2008\/09\/05\/optimistic-concurrency-updates-using-entity-framework-in-n-tier-and-n-layer-applications-part-2.aspx\" href=\"http:\/\/blogs.msdn.com\/cesardelatorre\/archive\/2008\/09\/05\/optimistic-concurrency-updates-using-entity-framework-in-n-tier-and-n-layer-applications-part-2.aspx\">http:\/\/blogs.msdn.com\/cesardelatorre\/archive\/2008\/09\/05\/optimistic-concurrency-updates-using-entity-framework-in-n-tier-and-n-layer-applications-part-2.aspx<\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>First of all, we are talking about using Entity Framework and how it fits within N-Tier and N-Layer applications, ok?, that\u2019s our initial scenario. An N-Tier application is an application where you have 3 or more physical tiers. I mean with that things like, \u201cPresentation\/Client Tier\u201d, \u201cApplication\/Business Server Tier\u201d and \u201cData Tier\u201d (a database server [&hellip;]<\/p>\n","protected":false},"author":362,"featured_media":12806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[4,12,48,101,109],"class_list":["post-1393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cesardelatorre","tag-net-3-5","tag-ado-net","tag-entity-framework","tag-visual-studio-2008","tag-wcf"],"acf":[],"blog_post_summary":"<p>First of all, we are talking about using Entity Framework and how it fits within N-Tier and N-Layer applications, ok?, that\u2019s our initial scenario. An N-Tier application is an application where you have 3 or more physical tiers. I mean with that things like, \u201cPresentation\/Client Tier\u201d, \u201cApplication\/Business Server Tier\u201d and \u201cData Tier\u201d (a database server [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/1393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/comments?post=1393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/1393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media\/12806"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media?parent=1393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/categories?post=1393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/tags?post=1393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}