如何使用int ID列更改表名ASP.net身份2.0吗? [英] How to change table names for ASP.net Identity 2.0 with int ID columns?

查看:100
本文介绍了如何使用int ID列更改表名ASP.net身份2.0吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ASP.net成员已有多年,我刚开始尝试ASP.net身份。他们刚刚发布了2.0版本,哪些是应该支持 INT 主键。我定义我的自定义身份类作为为了得到整数主键的支持如下:

I've used ASP.net membership for years and am just starting to try out ASP.net Identity. They just released version 2.0 and which is supposed to support int primary keys. I've defined my custom identity classes as follows in order to get integer primary key support:

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.AspNetUsers 表名和 dbo.AspNetRoles

When I create a database migration, it properly creates an integer Id column for each of the identity tables, however, it is ignoring my custom table names specified in the OnModelCreating method. Instead I get table names like dbo.AspNetUsers and dbo.AspNetRoles.

我也尝试添加 [表(表名)] 来我的自定义身份类和它没有任何效果。

I've also tried adding the [Table("TableName")] to my custom identity classes and it has no effect.

在寻找的ASP.net身份1.0的例子,它看起来像他们重复每个 modelBuilder.Entry&LT的;类型方式&gt;()ToTable(表名)使用基本身份类行:

In looking at examples for ASP.net Identity 1.0, it looked like they repeated each of the modelBuilder.Entry<Type>().ToTable("TableName") lines using the base Identity classes:

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.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]]'没有映射。检查类型尚未明确使用方法忽略或NotMappedAttribute数据注解排除。验证类型被定义为一类,是不是原始的或一般的,不从EntityObject继承。

怎样才能在ASP.net身份2.0整数主键自定义的表名?

How can I generate custom table names with integer primary keys in ASP.net Identity 2.0?

推荐答案

哎呀......所以在这浪费了几个小时后,我复制我的规则并粘贴它们下面的基地。 OnModelCreating(模型构建器); 行,一切工作正常。我没有意识到,基方法需要首先调用:(

Grrr...... So after wasting a couple hours on this, I copied my rules and pasted them below the base.OnModelCreating(modelBuilder); line and everything worked properly. I didn't realize that the base method needed to be called first :(

一切我现在能正常使用以下配置:

Everything is working properly now using the following configuration:

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

这篇关于如何使用int ID列更改表名ASP.net身份2.0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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