如何更改ASP.net具有整数ID列的www.example.com Identity 2.0的表名?

mwg9r5ms  于 2023-01-18  发布在  .NET
关注(0)|答案(2)|浏览(114)

我已经使用ASP.net会员资格多年,现在才开始试用ASP.NET Identity。他们刚刚发布了2.0版本,应该支持int主键。为了获得整数主键支持,我定义了自定义标识类,如下所示:

public class User : IdentityUser<int, UserLogin, UserRole, UserClaim>
{  
}

public class UserLogin : IdentityUserLogin<int>
{
}

public class Role : IdentityRole<int, UserRole>
{
    public string Description { get; set; }  // Custom description field on roles
}

public class UserRole : IdentityUserRole<int>
{
}

public class UserClaim : IdentityUserClaim<int>
{
}

public class MyDbContext : IdentityDbContext<User, Role, int, UserLogin, UserRole, UserClaim>
{
    public MyDbContext() : this("MyDB") { }

    public MyDbContext(string connStringName) : base(connStringName)
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<User>().ToTable("Users");
        modelBuilder.Entity<Role>().ToTable("Roles");
        modelBuilder.Entity<UserRole>().ToTable("UserRoles");
        modelBuilder.Entity<UserLogin>().ToTable("UserLogins");
        modelBuilder.Entity<UserClaim>().ToTable("UserClaims");

        base.OnModelCreating(modelBuilder);
    }
}

当我创建数据库迁移时,它为每个标识表正确地创建了一个整数Id列,但是,它忽略了我在OnModelCreating方法中指定的自定义表名,而是得到了dbo.AspNetUsersdbo.AspNetRoles这样的表名。
我还尝试将[Table("TableName")]添加到我的自定义标识类中,但没有效果。
在查看www.example.com Identity 1.0的示例时ASP.net,他们似乎使用基本Identity类重复了每一行modelBuilder.Entry<Type>().ToTable("TableName")

modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<IdentityUser<int, IdentityUserLogin<int>, IdentityUserRole<int>, IdentityUserClaim<int>>>().ToTable("Users");

modelBuilder.Entity<Role>().ToTable("Roles");
modelBuilder.Entity<IdentityRole<int, UserRole>>().ToTable("Roles");

modelBuilder.Entity<UserRole>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserRole<int>>().ToTable("UserRoles");

modelBuilder.Entity<UserLogin>().ToTable("UserLogins");
modelBuilder.Entity<IdentityUserLogin<int>>().ToTable("UserLogins");

modelBuilder.Entity<UserClaim>().ToTable("UserClaims");
modelBuilder.Entity<IdentityUserClaim<int>>().ToTable("UserClaims");

但是,我尝试了几种变体,但我的迁移都无法生成。我不断收到类似以下内容的错误:类型Microsoft.AspNet.Identity.实体框架.身份用户'4 [System.Int32,Microsoft. AspNet.身份.实体框架.身份用户登录' 1 [System.Int32],Microsoft.AspNet.身份.实体框架.身份用户角色'1 [System.Int32],未MapMicrosoft.AspNet.Identity.EntityFramework.IdentityUserClaim'1[System.Int32]]'。请检查是否未使用Ignore方法或NotMappedAttribute数据注解显式排除该类型。验证该类型是否定义为类,不是基元或泛型类型,并且不是从EntityObject继承的。
如何在www.example.com Identity 2.0中生成具有整数主键的自定义表名ASP.net?

ygya80vv

ygya80vv1#

所以在这上面浪费了几个小时之后,我复制了我的规则并将它们粘贴到base.OnModelCreating(modelBuilder);行下面,一切都正常工作了。
现在,使用以下配置时,一切工作正常:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder); // This needs to go before the other rules!

    modelBuilder.Entity<User>().ToTable("Users");
    modelBuilder.Entity<Role>().ToTable("Roles");
    modelBuilder.Entity<UserRole>().ToTable("UserRoles");
    modelBuilder.Entity<UserLogin>().ToTable("UserLogins");
    modelBuilder.Entity<UserClaim>().ToTable("UserClaims");
}
w80xi6nr

w80xi6nr2#

在IdentityModel.cs文件中,我向ApplicationDbContext类添加了以下代码
请注意,型号名称与已接受答案中列出的名称不同

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

        modelBuilder.Entity<ApplicationUser>().ToTable("SystemUsers");
        modelBuilder.Entity<IdentityRole>().ToTable("SystemRoles");
        modelBuilder.Entity<IdentityUserRole>().ToTable("SystemUserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("SystemUserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("SystemUserClaims");
    }

相关问题