加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 实体框架种子与身份(Microsoft.Owin.Security)用

发布时间:2020-12-16 07:06:04 所属栏目:asp.Net 来源:网络整理
导读:我有一个为数据库设置种子的类,它为2个用户添加了角色和自定义字段.我遇到的问题是它将数据保存在[dbo].[AspNetUsers]而不是[dbo].[IdentityUsers].两个表都已创建.播种时,数据进入AspNetUser.当我启动网站并注册新用户时,数据将进入IdentityUser. 这是Migra
我有一个为数据库设置种子的类,它为2个用户添加了角色和自定义字段.我遇到的问题是它将数据保存在[dbo].[AspNetUsers]而不是[dbo].[IdentityUsers].两个表都已创建.播种时,数据进入AspNetUser.当我启动网站并注册新用户时,数据将进入IdentityUser.

这是Migration类:

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模型的自定义字段.

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配置类.

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

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

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 : https://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 });

    }

我的问题是:为什么我有从Identity表复制的AspNet前缀表名?为什么种子在Web应用程序使用另一种时使用一种?

解决方法

您的DbContext是否继承自IdentityDbContext?

如果从IdentityDbContext继承,则不需要在OnModelCreating中进行任何其他配置. IdentityDbContext提供的映射和默认的实体框架约定应该足够了.

如果在没有UserId的setter的情况下声明ApplicationUser,则不需要忽略UserId属性:

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):

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表具有自定义字段).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读