10 Entity Framework 7 Features That You Must Know

In previous article Entity Framework 7 In-Memory Provider (for testing) simplified, we discussed  how in-memory provider help to test the application without writing code for mocking it up. Instead of using real data store, In memory provider will use memory to store data temporary.

EF7 is more powerful and has significant changes over Entity Framework 6.x. Rowan Miller recently announced new release of Entity Framework 7 beta 8 on his blog. Lot of features are still missing in current release, you can read EF 7 Roadmap on github.

Entity Framework 7 will give you familiar developer experience to previous versions of EF, the user can still work with DbContext, DbSet, etc. EF7 is much more lightweight than previous versions and is built from the ground up to work great in the cloud (using ASP.NET vNext) on devices (i.e. in universal Windows apps) as well as in traditional .NET scenarios.

This article outlines brief introduction to top 10 important features of Entity Framework 7.

Lightweight and extensible

Instead of use existing Entity Framework 6 APIs, Team decide to start developing from the scratch. You can use only that extensions which are useful to your project. The pattern and concept remain same to use Entity Framework. You can use DbContext/DbSet same way as you are currently using. The advantage of extensible is you can replace and extend it.

New Platforms

EF is most popular ORM that currently includes applications built with technologies such as WPF, WinForms and ASP.NET. After looking at future Microsoft has decided to support remaining platforms where .NET development is common. This includes Windows Store, Windows Phone and the Cloud Optimized .NET.

New Data Stores

Entity Framework was clearly tied with relational data stores. Now onward EF provide great support to non relational data stores also. Relational & non Relational, like Azure table storage.

Optimized Query generation

Based on people's higher request on EF uservoice "Improved SQL Generation", Diego Vega (Engineering Manager, Entity Framework) responded positively and start working on this feature. On next or may be final release they will include this feature. EF7 will generate much simpler SQL queries than EF6 for the most common scenarios.

Code first only

Finally Microsoft Team retired EDMX in Entity Framework 7. You can read Rowan Miller's article EF7 - What Does “Code First Only” really mean. If you still love edmx, you would love to read What about EDMX when EF7 arrives? written by Julie Lerman.

Batch Update

No longer need to use EF batch update utilities to perform batch operations because EF7 has inbuilt support for that. EF 7 no longer send individual command for every insert/update/delete statement. EF 7 will batch multiple statements in single round trip to the database.

Unique Constraints

EF 7 allows you to identify additional unique keys within your entities in addition to the primary key. You can then use these alternate keys as the target of foreign key relationships. A unique constraint is introduced for each alternate key in the model.

How it look like

By convention, the index and constraint that are introduced for an alternate key will be namedAK_<type name>_<property name>. For composite alternate keys <property name> becomes an underscore separated list of property names.

How to use it

You have to use fluent API because a unique constraint can not be configured using Data annotations.

  1. class MyContext: DbContext  
  2. {  
  3.     public DbSet < Employee > Employees  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.   
  9.     protected override void OnModelCreating(ModelBuilder modelBuilder)  
  10.     {  
  11.         modelBuilder.Entity < Employee > ()  
  12.             .HasAlternateKey(e => e.EmployeeCode)  
  13.             .HasName("AlteranteKey_EmployeeCode");  
  14.     }  
  15. }  
  16.   
  17. class Employee  
  18. {  
  19.     [Key]  
  20.     public int EmployeeId  
  21.     {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     public string Name  
  26.     {  
  27.         get;  
  28.         set;  
  29.     }  
  30.     public string EmployeeCode  
  31.     {  
  32.         get;  
  33.         set;  
  34.     }  
  35.     public DateTime DateOfBirth  
  36.     {  
  37.         get;  
  38.         set;  
  39.     }  
  40. }  
In-Memory provider (for testing)

To test Entity Framework DB operations, we need to mock out DbContext which is not an easy task. In-memory store is useful to test data context without mock out it. In-memory store will behave same as a real data store. You can perform the same operations as In-memory providers..

Logging

Capture all interaction with the database using EF7 inbuilt Logging feature. EF7 logging feature is powered by Microsoft.Framework.Logging and Microsoft implemented ILogingFactory that may support all .NET platform (hope it will work). Take a look at very useful discussion already started on Stackoverflow

Shadow Properties

Shadow properties are properties that do not exist in entity class, but treated as part of it. The value and state of these properties is maintained purely in the Change Tracker. These properties can participate in all database operations, but should not be exposed via entity class to the rest of the application. Currently you can declare shadow properties using fluent API only.

EF 7 useful Resources 
My original blog url

Up Next
    Ebook Download
    View all
    Learn
    View all