New Features of Entity Framework 6 Beta

Introduction

 
In this article, I provide some exciting and useful features of the Entity Framework 6 beta release. Currently, EF 6's beta release is available. The Beta release can be downloaded from NuGet. These features will be really useful and when the final release is available you will really find them useful and more powerful than the previous versions. So without wasting time I will proceed to discuss some key features.
  1. Asynchronous pattern Support
     
    The .NET Framework 4.5 introduced the Task-Based Asynchronous Pattern that uses the async modifier and the await operator to provide asynchronous programming. This feature is now supported by EF6. So database calls that fetch a large amount of data, made through EF can be processed asynchronously. By using the asynchronous pattern we can avoid blocking the responsiveness of our application.
     
    Let's have at look some code snippets without and with the asynchronous pattern and how to use this pattern with EF6.
     
    a. Without Asynchronous Patterns
    1. private static void SaveInput(string question,string answer)  
    2. {  
    3.     using (var ctx = new AnswersContext())  
    4.     {  
    5.         ctx.Questions.Add(new Question()  
    6.         {  
    7.             Content = question,  
    8.             Answers =new List<Answer> { new Answer { Content = answer } }  
    9.         });  
    10.         ctx.SaveChanges();  
    11.     }  
    b. With Asynchronous Patterns
    1. private static void PrintQuestions()  
    2. {  
    3.     using (var ctx = new AnswersContext())  
    4.     {  
    5.         foreach (var q in ctx.Questions)  
    6.         {  
    7.             Console.WriteLine(q.Content);  
    8.             foreach (var a in q.Answers)  
    9.             {  
    10.                 Console.WriteLine(" -" + a.Content);  
    11.             }  
    12.         }  
    13.     }  
    As you can see, I have used async and await, two new keywords. If we are using the await operator then it is necessary to use async with the method signature. Since we are calling the ToListAsync method, as soon as this method is called, the thread won't block until the task finishes.
     
  2. Custom Conventions
     
    When we use the code-first approach in Entity Framework, classes are mapped with the database using a set of conventions that are defined in EF, such as which property becomes the primary key, which name of in the table maps with an entity name etcetera. We must use some custom code conventions for that. Sometimes these default conventions are not perfect for our model and we must do configuration using Data annotation and other ways.
     
  3. Code First Mapping to Insert/Update/Delete Stored Procedures
     
    When we use code first entities, it is easy to select data using Stored Procedures but there was no way to delete, update and insert data using Stored Procedures.
     
    EF 6 provides this ability to insert, update and delete using a Stored Procedure. So you can do that as in the following:
    1. mBuilder .Entity<Users>() .MapToStoredProcedures(s => {  
    2. s.Update(u => u.HasName("modify_User"));  
    3. s.Delete(d => d.HasName("delete_User"));  
    4. s.Insert(i => i.HasName("insert_User"));  
    5. }); 
  4. Connection Resiliency
     
    Since we know that currently, most database servers are a cloud, such as an Azure Database. So the chances of connection failures are greater. This could be because of some defensive techniques used by the cloud database because of how the cloud database maintains its consistency, so to maintain that sometimes it closes the connection explicitly.
     
    EF 6 provides many ways to retry actions to be performed on the database and to minimize the number of failures and the effect of defensive code. Even users can create their own retry strategies and decide which action should be performed if an exception occurs.
     
  5. Better supports of POCO entities
     
    Now we can make nesting of entities and complex types inside classes. We can also have Custom Equal and GetHashCode Method implementations.
     
  6. Multiple Context Per Database
     
    In EF 5, when we use Migration or a Code first database created automatically then we can have only one code first model per database. But In EF 6, we will be able to have multiple code first models per database.
     
  7. Default Isolation level
     
    Default isolation level has been changed for databases created using code first in EF6. In EF 5 it was Serializable so now it has been changed to READ_COMMITTED_SNAPSHOT. So because of this isolation level, the database becomes more scalable and there are very few chances of deadlock.
     
  8. Supports Enum and Spatial data types :
     
    EF 6 now provides support of Enum and Spatial Data Types.
     
  9. Now easy to set default schema at one place
     
    EF 6 provides the DbModelBuilder.HasDefaultSchema Code first API that allows configuration of a default database schema for the code fist model in one place. Previously this default schema was hard-coded to dbo and if we needed to change it, we needed to do a lot of work.

Summary

 
In this article, I have discussed some key features that have been introduced in the Beta release of Entity Framework 6. There are many other features also introduced in EF 6. For more information, you can refer to the following Codeplex website.
 
References
 
http://entityframework.codeplex.com/


Similar Articles