我用过ASP.net会员资格,我刚刚开始try ASP.网络身份.他们刚刚发布了2.0版,该版本应该支持int个主键.为了获得integer主键支持,我定义了如下自定义标识类:

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

当我创建数据库迁移时,它会正确地 for each 标识表创建一个整数Id列,但是它会忽略在OnModelCreating方法中指定的自定义表名.相反,我得到了dbo.AspNetUsersdbo.AspNetRoles这样的表名.

我还try 将[Table("TableName")]添加到我的自定义标识类中,但没有效果.

在查看ASP.net Identity 1.0的示例时,看起来它们使用基本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");

然而,我try 了几种变体,但我的迁移并没有产生任何效果.我不断收到类似于The type Microsoft.AspNet.Identity.EntityFramework.IdentityUser'4[System.Int32,Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin'1[System.Int32],Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole'1[System.Int32],Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim'1[System.Int32]]' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject.的错误

如何在ASP中使用整型主键生成自定义表名.网络身份2.0?

推荐答案

吼叫声所以在这件事上浪费了几个小时后,我复制了我的规则,并将它们粘贴到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");
}

Asp.net相关问答推荐

Razor中的共享本地化资源文件

在 Web.Config 中模拟标签

分层架构中的 ASP.NET 和实体框架 - 仅将实体框架用于 ORM

强制从 /bin 而不是 GAC 加载程序集?

asp.net、url 重写模块和 web.config

在哪里可以记录 ASP.NET Core 应用程序的启动/停止/错误事件?

在构建时自动停止/重新启动 ASP.NET 开发服务器

即使使用正确的 Accepts 标头,WebAPI 也不会返回 XML

如何在 FileUpload 控件中限制文件类型

使用 IIS 的 ASP.NET 调试超时

如何将 global.asax 文件添加到 ASP.NET MVC4 项目?

在 Asp.net Rowcommand 事件中获取行索引

每个 'HttpRequest' 在 ASP.NET 中都有自己的线程吗?

c# list 如何在两个值之间插入一个新值

路由模板分隔符/不能连续出现 - 属性路由问题

当用户在文本框中按 Enter 键时执行按钮单击事件

如何直接在 .aspx 页面中访问 web.config 设置?

通过 CultureInfo 格式化字符串

在 ASP.NET Web API 2 中禁用 *all* 异常处理(为我自己腾出空间)?

获取 Application_Start 中的当前应用程序物理路径