A quick look at Entity Framework 7

Just a short post to recollect a few things I gathered from Pluralsight course Looking ahead to EF7.

Example project

Based on the demos of the course, I put together a small example application, using Entity Framework 7 beta6. The code is found on my github repository.

Configuring the DbContext

EF7 supports two ways to configure the DbContext:

  1. directly in the DbContext class
  2. by injecting the configuration options into the context’s constructor

Configuration in the DbContext

In this case, the context configures itself, in this case to use an in-memory database, with the OnConfiguring() method being invoked by EF.

namespace Context.EF7
{
    using Domain;
    using Microsoft.Data.Entity;
    using Microsoft.Data.Entity.Infrastructure;

    public class VideoStoreContext: DbContext
    {
        public DbSet<Movie> Movies { get; set; }
        public DbSet<Actor> Actors { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseInMemoryDatabase();
        }
    }
}

Injecting the options into the DbContext

The alternative is to pass the options as a constructor parameter to the context.

namespace Context.EF7
{
    using Domain;
    using Microsoft.Data.Entity;
    using Microsoft.Data.Entity.Infrastructure;

    public class VideoStoreContext: DbContext
    {
        public DbSet<Movie> Movies { get; set; }
        public DbSet<Actor> Actors { get; set; }
 
        public VideoStoreContext(DbContextOptions options) : base(options)
        {

        }
    }
}

Providers

In the example above, we saw the use of the optionsBuilder:

optionsBuilder.UseInMemoryDatabase();

It is worth noting that UseInMemoryDatabase() is an extention method that gets pulled in when we include the entityframework.inmemory package.

To use SQL Server instead, we would include the package entityframework.sqlserver and configure the context as such, using the UseSqlServer() extension method from that dll:

optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb; Database=EF7Demo; Trusted_Connection=True;");

Summary

When looking at both options above, we see that configuring the context with OnConfiguring() locks the underlying provider (as well as other options).

The second way, by injecting the options into the constructor, is much more flexible. This approach should be preferred to using OnConfiguring().

That way, we can, for example, use SQL Server for the application and use an in-memory provider for the tests.

The application code would look something like this:

var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer("Server = (localdb)\\mssqllocaldb; Database=EF7Samurai; Trusted_Connection=True;");

using (var context = new VideoStoreContext(optionsBuilder.Options))
{
    // do cool stuff with the database here
}

And in the tests, we use an in-memory database:

var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseInMemoryDatabase(); // extension method from package entityframework.inmemory

var context = new VideoStoreContext(optionsBuilder.Options);
// test stuff
context.Dispose();

Pretty cool, huh?