Entity Framework Core with .Net 4.7 MVC Identity Issues

asp.net-identity asp.net-identity-3 asp.net-mvc entity-framework-core


I am looking to replace EF6 with EF Core in a project i have just started building.

The issue i am facing is that currently i am inheriting my Context from IdentityDbContext like so using Identity.

public partial class MyContext : IdentityDbContext<ApplicationUser>

Moving to EF Core requires that i use Identity Core. This is fine in my data access layer as i can just reference Microsoft.AspNetCore.Identity.EntityFrameworkCore instead.

The real issue i discovered was when trying to use this in my MVC .Net 4.7 application where Identity Core will not work as it is not .Net Core.

So is my best option to just split Identity into its own context or is there a way that i can use EF Core as i currently am by inheriting the IdentityContext using non Identity Core?

Or have i got the complete wrong end of the stick...


6/28/2018 9:59:16 PM

Popular Answer

What I just did was to re-implement the functionality from IdentityDbContext straight on to my DbContext + the provided user stores. There is really no magic involved. The only thing the IdentityDbContext does is to add a few datasets and some mappings to your context.

You can find everything you need here: https://github.com/aspnet/AspNetIdentity/tree/master/src/Microsoft.AspNet.Identity.EntityFramework

We are using Guids as Ids to we already had custom classes like CustomUserLogin which inherited from IdentityUserLogin so I just moved everything from the base class into that class.

One thing you will see is that the old code doesn't have proper navigation properties in both directions. I added this so I could to the mapping like this:

    private void SetupIdentityTables(ModelBuilder modelBuilder)
        var user = modelBuilder.Entity<ApplicationUser>()
        user.HasMany(u => u.Roles).WithOne().HasForeignKey(ur => ur.UserId);
        user.HasMany(u => u.Claims).WithOne().HasForeignKey(uc => uc.UserId);
        user.HasMany(u => u.Logins).WithOne().HasForeignKey(ul => ul.UserId);
        user.Property(u => u.UserName)
        user.HasIndex(x => x.UserName).HasName("UserNameIndex").IsUnique();

        // CONSIDER: u.Email is Required if set on options?
        user.Property(u => u.Email).HasMaxLength(256);

            .HasKey(r => new { r.UserId, r.RoleId });

            .HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId });


        var role = modelBuilder.Entity<CustomRole>()
        role.Property(r => r.Name)
        role.HasIndex(x => x.Name).HasName("RoleNameIndex").IsUnique();
        role.HasMany(r => r.Users).WithOne().HasForeignKey(ur => ur.RoleId);

NOTE: This code is actually not working perfectly as it has some foreign key problems. I will need to take another look at the mappings but you should get the idea at least.

2/21/2019 10:10:30 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow