【问题标题】:How to extend IdentityRole into a custom table name如何将 IdentityRole 扩展为自定义表名
【发布时间】:2014-10-03 14:57:52
【问题描述】:

我正在尝试为我的 MVC 5、Identity 2.0 项目实施基于角色的安全性。 我一直在网上看一些教程,但有点卡住了。

我创建了以下内容:

public class Role : IdentityRole
{
    public Role(string name) : base(name)
    { }

    public Role()
    { }

    public string Description { get; set; }
    public string MenuIcon { get; set; }
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
}

和我的背景:

    new public DbSet<Role> Roles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        base.OnModelCreating(modelBuilder);

        //rename the tables and columns used by the Identity Framework
        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<UserRole>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
    }

和我的种子方法:

    RoleManager roleManager = new RoleManager(new RoleStore<Role>(context));
    roleManager.Create<Role, string>(new Role("Orders"));

但是,当我运行应用程序时,它会为角色创建包含 2 个表的数据库:Role(我希望角色存在的位置,它包含我的自定义属性)和 AspNetRoles。它确实使用我在 Seed 方法中创建的角色填充两个表。

我到底做错了什么,它正在创建 2 个表?

【问题讨论】:

  • 我也有类似的问题。你终于找到解决方案了吗?

标签: c# asp.net-mvc-5 asp.net-identity


【解决方案1】:

所有 asp.net 表都应该识别新表名,而不仅仅是更改后的表。

用户表:

public class User : IdentityUser<string, UserLogin, UserRole, UserClaim>
{
    // Your properties
}

UserLogin 表:

public class UserLogin : IdentityUserLogin { }

用户角色表:

public class UserRole : IdentityUserRole { }

UserClaim 表:

public class UserClaim : IdentityUserClaim { }

角色表:这顶帽子要继承自IdentityRole&lt;string, UserRole&gt;

public class Role : IdentityRole<string, UserRole> 
{ 
    //Any relevant properties

    public string Description { get; set; }
    public string MenuIcon { get; set; }
    public string ControllerName { get; set; }
    public string ActionName { get; set; }    
}

数据上下文:这必须继承自IdentityDbContext&lt;User, Role, string, UserLogin, UserRole, UserClaim&gt;

public class YourDataContext : IdentityDbContext<User, Role, string,   UserLogin, UserRole, UserClaim>
{
    // Your DbSet items

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

        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<UserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<UserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<UserRole>().ToTable("UserRole");

        modelBuilder.Entity<User>().Property(r => r.Id);
        modelBuilder.Entity<UserClaim>().Property(r => r.Id);
        modelBuilder.Entity<Role>().Property(r => r.Id);

       //Add your new properties of Role table in here.


    }
}

在这个例子中,我使用了表的 ID 作为stringtype。

希望这会有所帮助。

【讨论】:

  • 谢谢!我遇到的问题是Error 2 The type 'Express.Models.Role' cannot be used as type parameter 'TRole' in the generic type or method 'Microsoft.AspNet.Identity.EntityFramework.RoleStore&lt;TRole&gt;'. There is no implicit reference conversion from 'Express.Models.Role' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityRole'. 我也收到User 的类似消息。
  • 我也在使用身份框架 2.0,但这对我有用。确保正确放置两种类型的角色表“UserRole”和“Role”。检查此stackoverflow.com/questions/22682017/…。还要检查这个链接aspnetidentity.codeplex.com/workitem/2126
  • 谢谢!顺便说一句,您实际上不必更改表名,这不是必需的,我也没有添加那些 modelBuilder.Entity&lt;User&gt;().Property(r =&gt; r.Id); 如果您尝试向 AspNetRoles 表添加属性,我注意到的是一个小“错误”,代码-第一次迁移有时它必须删除它并再次添加它。您可以安全地编辑迁移并添加列。哦,我也遇到了 Lock 问题,但我只需要更改如何创建 UserStore 和 Manager。
【解决方案2】:

删除这一行:

new public DbSet<Role> Roles { get; set; }

然后你必须用你自己的列替换默认的 AspNetRoles 表列。

var applicationRole = modelBuilder.Entity<ApplicationRole>().ToTable("Roles");
applicationRole.Property(r => r.Name).HasColumnName("Description");
...

但是,如果您想自定义 AspNet Identity 配置文件信息,最好创建新的相关表 (check this link)。

【讨论】:

  • 谢谢。不幸的是,这没有帮助。它做了同样的事情——创建了两个表,并在两者中创建了角色。
  • @Lock 你确定删除“new public DbSet Roles { get; set; }”表“Role”仍然是beign创建的吗?
  • 您好。是的,我确实删除了那个位。
  • 嗨,我向 ASPRoles CREATE TABLE [dbo].[AspNetRoles] [Id] [nvarchar](128) NOT NULL, [Name] [nvarchar](256) NOT NULL, [Description] [nvarchar](256) NOT NULL, [CompanyId] [nvarchar](256) NOT NULL, 添加了更多属性,现在我的创建 IdentityRole 失败,我该如何处理?
猜你喜欢
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 2015-10-03
  • 2023-03-14
相关资源
最近更新 更多