Entity Framework Seeding with Identity (Microsoft.Owin.Security) user

元气小坏坏 提交于 2019-12-05 10:32:46

Are your DbContext inheriting from IdentityDbContext?

If you inherit from IdentityDbContext you should not need any additional configuration in your OnModelCreating. The mappings provided by the IdentityDbContext and the default Entity Framework conventions should suffice.

If you declare your ApplicationUser without a setter for the UserId, then you won't need to ignore the UserId property:

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        Email = "";
        Language = "";
    }

    public string Email { get; set; }
    public string Language { get; set; }

    public string UserId
    {
        get { return Id; }
    }
}

Then your ApplicationDbContext can be as simple as this (I have renamed your Configuration class to MigrationConfiguration for clarity) :

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    static ApplicationDbContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, MigrationConfiguration>());
    }

    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

This works as expected (only tables named AspNetXXXX, the AspNetUsers table has the custom fields) with the release versions (Version 1.0.0) of the AspNet.Identity.Core and AspNet.Identity.EntityFramework assemblies.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!