Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ADO.NET Entity Framework 4: New and Notable Features : Page 2

Leverage the new and enhanced features of ADO.NET Entity Framework 4.0 to model at the right level of abstraction.


advertisement

Support for Persistence Ignorance

Support for persistence ignorance is one of the most striking features in Entity Framework 4. What is it, you ask? Persistence ignorance is a concept introduced in Entity Framework 4 that encapsulates the underlying details of an object while it is being persisted in a persistent medium. So, you can design your conceptual model without having to be bothered about the intricacies of the underlying relational model.

You can also design your own Plain Old CLR Objects (commonly known as POCOs). POCOs are simple objects that are not dependent on any specific persistent technology. Here is an example of a POCO class:



public class Product { public Int32 ProductID { get; set; } public String ProductName { get; set; } public Int32 SupplierID { get; set; } public Int32 UnitQuantity { get; set; } public decimal UnitPrice { get; set; } public Int32 UnitsInStock { get; set; } public Int32 UnitsOnOrder { get; set; } public Int32 ReorderLevel { get; set; } public bool Discontinued { get; set; } }

Another great feature in Entity Framework 4 is the ability to design the Object Model first (using the Visual Studio 2010 Entity Data Model Designer, if you use VS2010) and then use it to design the Relational Model. This ability is commonly known as Model First Development.

Handling Concurrency Conflicts

The Entity Framework supports optimistic concurrency by default. This implies that by default it doesn't check for concurrency while updating data. You can, however, change this behavior. To do so, you need to use the property called ConcurrencyMode in your Entity Data Model. Here's an example:

<Property Name="LastUpdatedDateTime" Type="DateTime" Nullable="false" ConcurrencyMode="Fixed" />

When you use this attribute on your Entity in the Entity Data Model and a conflict occurs, an OptimisticConcurrencyException is thrown. To handle this exception, you can use the Refresh() method of the ObjectContext class. The Refresh() method has the following signature:

public void Refresh(RefreshMode refreshMode,Object entity)

The RefreshMode enumeration can accept one of the following two values:

  • ClientWins
  • StoreWins

The following code snippet illustrates how you can handle this:

try { //Some code to save data dataContext.SaveChanges(); Console.WriteLine("No conflicts, data saved successfully."; } catch (OptimisticConcurrencyException) { dataContext.Refresh(RefreshMode.ClientWins, orders); dataContext.SaveChanges(); Console.WriteLine("OptimisticConcurrencyException has been handled and the changes saved successfully."); }

To use StoreWins, you can simply pass RefreshMode.StoreWins as a parameter to the Refresh() method of the data context as shown below:

try { //Some code to save data dataContext.SaveChanges(); Console.WriteLine("No conflicts, data saved successfully."; } catch (OptimisticConcurrencyException) { dataContext.Refresh(RefreshMode.StoreWins, orders); dataContext.SaveChanges(); Console.WriteLine("OptimisticConcurrencyException has been handled and the changes saved successfully."); }

One of the best strategies to handle concurrency conflicts in Entity Framework is to have an extra column of type TimeStamp in your database table and then change the ConcurrencyMode of this column to Fixed in the Entity Data Model.

Suggested Readings

Here are a few links to further study on this topic:

Summary

ADO.NET Entity Framework 4.0 is a more mature ORM tool than its predecessors. In this two-part series on Entity Framework, I explored the basics, explained how to program against the Entity Data Model and explored some of its other notable features. You can learn more about ADO.NET Entity Framework 4.0 from my book, "Entity Framework Tutorial".



Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap