Press "Enter" to skip to content

Using ASP.NET Identity 3 with Entity Framework 6

Overview

 

I put together a provider for Entity Framework 6 and ASP.NET Identity 3. This post introduces it.

ASP.NET Identity 3 is awesome and packed with new features. Me and my team (OneBit Software) – we are comfortable and very keen to use it in our projects.

ASP.NET Identity 3 by default uses Entity Framework 7 (together with other details, such as ASP.NET 5, MVC 6, DNX, etc. etc.). EF7 is also great and stable, but very feature-poor at the moment. We are not ready to use EF7 (at the time of writing this post) in production, even though there is go-live production support for it.

Entity Framework 6 for that matter is stable and we trust it.

So the long story short – we need ASP.NET Identity 3 with support for Entity Framework 6. The solution is the Microsoft.AspNet.Identity.EntityFramework6 project.

GitHub source: https://github.com/OneBitSoftware/Microsoft.AspNet.Identity.EntityFramework6

At the time of launching the project is built for RC1-Update1. All nugget packages are fixed to the 1.0.0-rc1-final version.

The solution is structured with the core source, unit tests and a sample web application:

 

Entity Framework 6

 EF6 doesn’t support generic types for DbSet’s. You can’t do:

DbSet<IdentityUser<TKey>> Users;

This adds some complexity, because Identity 3 aims to use generic types to support generic keys for the database.

There’s some further complexity in other features/limitations of EF6 in itself, so some parts of Identity 3 code need tweaking, like the OnModelCreating initializer.

 

ASP.NET Identity 3

I’ve spent quite some time with this lovely library that it has become my weekend hobby. I’ve done my best to reuse as much as I can and do as little as possible to get it running with EF6.

The database is assuming the Id columns (TKey) are of type string. You can modify that to your liking. I’ve done it because the default implementation uses Guids.

There are no changes to the way ASP.NET Identity 3 works internally, all modifications are around how users and roles are stored.

 

Getting Started: Sample ASP.NET 5 MVC 6 solution project with ASP.NET Identity 3 and Entity Framework 6

You will find a complete sample with everything set up in the repository: https://github.com/OneBitSoftware/Microsoft.AspNet.Identity.EntityFramework6/tree/master/samples/IdentitySamples.MVC6.EF6

If you need to add this to your own MVC project, just reference Microsoft.AspNet.Identity.EntityFramework6 and place the following in the ConfigureServicesmethod of your Startup.cs file:

//Inject ApplicationDbContext in place of IdentityDbContext and use connection string

services.AddScoped<IdentityDbContext<ApplicationUser>>(context =>

    newApplicationDbContext(Configuration[“Data:DefaultConnection:ConnectionString”]));

 

//Configure Identity middleware with ApplicationUser and the EF6 IdentityDbContext

 services.AddIdentity<ApplicationUser, IdentityRole>(config =>

{

    config.User.RequireUniqueEmail = true;

})

.AddEntityFrameworkStores<IdentityDbContext<ApplicationUser>>()

.AddDefaultTokenProviders();

 

You can use ApplicationUser and IdentityDbContext to modify the model further.

This is currently not released as a Nuget package, although if there is interest we could do that on MyGet.

Dependencies

The Test project requires Microsoft.AspNet.Testing and Microsoft.AspNet.Identity.Test nuget packages. These are available on the MyGet aspnetmaster package source here: https://www.myget.org/F/aspnetmaster/api/v2/

In order for this to be easy to get going, I have included them in the “lib” folder. The “project.json” files in the “wrap” folder point to these local assemblies.

 Unit Testing

 

At the time of releasing, the solution passes all unit tests.  Make sure they run for you too if you are contributing to the repository.

Contribution, Issues and Discussions

I would love it if you make a fork and create a pull request. Let’s keep all discussions and issues in GitHub. Anything that you need to be updated or fixed – let us know and if time allows for it we will look into it. Feel free to follow the patterns and contribute.

We will keep a close eye on updates to ASP.NET Identity 3, especially around RC2 and a final version.