10 Entity Framework 7 (EF core 1.0) features that you must know

In previous article Entity Framework 7 In-Memory Provider (for testing) simplified, we discuss 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 announce 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 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 decide 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 generates 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's look like?
By convention, the index and constraint that are introduced for an alternate key will be named AK_<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 constraints can not be configured using Data annotations.
class MyContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Employee>()
            .HasAlternateKey(e => e.EmployeeCode)
            .HasName("AlteranteKey_EmployeeCode");
    }
}

class Employee
{
    [Key]
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string EmployeeCode { get; set; }
    public DateTime DateOfBirth { get; set; }
}    

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. You can read  more at  Entity Framework 7 In-Memory Provider (for testing) simplified.

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 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. Read the full Shadow properties documentation.

Updates

Microsoft recently renamed Entity Framework 7 to Entity Framework Core 1.0


EF 7 useful Resources