具有身份的实体框架播种(Microsoft.Owin.Security)用户 [英] Entity Framework Seeding with Identity (Microsoft.Owin.Security) user

查看:68
本文介绍了具有身份的实体框架播种(Microsoft.Owin.Security)用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为数据库添加种子的类,该类添加了2个具有角色和自定义字段的用户.我的问题是它将数据保存在[dbo].[AspNetUsers]中,而不是在[dbo].[IdentityUsers]中.这两个表均已创建.播种后,数据进入AspNetUser.当我启动网站并注册新用户时,数据将进入IdentityUser.

I have a class that seed the database which add 2 users with roles and custom fields. The problem I have is that it save the data in [dbo].[AspNetUsers] instead of [dbo].[IdentityUsers]. Both tables are created. When seeded, the data go into AspNetUser. When I launch the web site, and register a new user, the data go into IdentityUser.

以下是迁移"类别:

internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(DatabaseContext context)
    {
        base.Seed(context);

        var userStore = new UserStore<ApplicationUser>();
        var manager = new UserManager<ApplicationUser>(userStore);

        var role = new IdentityUserRole { Role = new IdentityRole(Model.Roles.ADMINISTRATOR) };
        var user = new ApplicationUser() { UserName = "123123", Email = "123123@123.com", Language = "en-US"};
        user.Roles.Add(role);
        IdentityResult result = manager.Create(user, "123123");

        var role2 = new IdentityUserRole { Role = new IdentityRole(Model.Roles.NORMAL) };
        var user2 = new ApplicationUser() { UserName = "qweqwe", Email = "qweqwe@qweqwe.com", Language = "fr-CA" };
        user.Roles.Add(role2);
        IdentityResult result2 = manager.Create(user2, "qweqwe");
    }
}

这是ApplicationUser类,它为Identity模型定义自定义字段.

Here is the ApplicationUser class which define custom field for the Identity model.

public class ApplicationUser : IdentityUser, ICurrentUser
{
    public ApplicationUser()
    {
        Email = "";
        Language = "";
    }
    public string UserId {
        get { return base.Id; }
        set{} 
    }
    public string Email { get; set; } 
    public string Language { get; set; } 

}

这是此新类的Entity Framework配置类.

Here is the Entity Framework configuration class for this new class.

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.HasKey(d => d.Id);
        this.Ignore(d => d.UserId);
    }
}

两个都使用具有相同配置的save DataContext:

Both are using the save DataContext with the same configuration:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //... others entity here

        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());

        //Theses configuration are required since custom fields are added to ApplicationUser. Here is why : http://stackoverflow.com/questions/19474662/map-tables-using-fluent-api-in-asp-net-mvc5-ef6
        modelBuilder.Entity<IdentityUserLogin>().HasKey(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

    }

我的问题是:为什么我有与身份表重复的AspNet前缀表名称?为什么种子使用一个而Web应用程序使用另一个?

My question are: Why do I have AspNet prefixe tables name that is duplicated from Identity tables? Why does the seed use one while the web application use the other?

推荐答案

您的DbContext是否从IdentityDbContext继承?

Are your DbContext inheriting from IdentityDbContext?

如果从IdentityDbContext继承,则OnModelCreating中不需要任何其他配置. IdentityDbContext提供的映射和默认的Entity Framework约定应足够.

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.

如果您声明ApplicationUser的UserId没有设置器,那么您将不需要忽略UserId属性:

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; }
    }
}

然后,您的ApplicationDbContext可以很简单(为清楚起见,我将您的Configuration类重命名为MigrationConfiguration):

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")
    {
    }
}

这与AspNet.Identity.Core和AspNet.Identity.EntityFramework程序集的发行版本(版本1.0.0)一起正常工作(只有名为AspNetXXXX的表,AspNetUsers表具有自定义字段).

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.

这篇关于具有身份的实体框架播种(Microsoft.Owin.Security)用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆