【问题标题】:ASP.NET Identity - Error when changing User ID Primary Key default type from string to int AND when using custom table namesASP.NET Identity - 将用户 ID 主键默认类型从字符串更改为 int 以及使用自定义表名时出错
【发布时间】:2014-04-12 15:18:39
【问题描述】:

我正在使用 Microsoft.AspNet.Identity 2.0.0-beta1 和 Entity Framework 6.1.0-beta1(2014 年 2 月 11 日发布)。

当我尝试将用户 ID 主键的默认类型从字符串更改为 int AND 时,当我尝试使用自定义表名(因此 User.MyUsers 而不是dbo.AspNetUsers):

"实体类型 'IdentityUser' 和 'ApplicationUser' 不能共享表 'MyUsers',因为它们不在同一个类型层次结构中,或者它们之间没有有效的一对一外键关系和匹配的主键.

我可以成功地将用户 ID PK 的默认类型从 string 更改为 int 或更改默认身份表名称,但我不能同时一起进行而不遇到此错误。

我的解决方案基于:

1:来自http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx 的“使用户和角色的主键类型可扩展”部分。

2:How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

我的实际代码是:

using Microsoft.AspNet.Identity.EntityFramework;

namespace Musetone.Models
{
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
}

public class CustomRole : IdentityRole<int, CustomUserRole>
{
    public CustomRole() { }
    public CustomRole(string name) { Name = name; }
}

public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

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

        modelBuilder.Entity<IdentityUser>().ToTable("MyUsers", "User").HasKey(u => u.Id).Property(u => u.Id).HasColumnType("int");
        modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers", "User").HasKey(u => u.Id).Property(u => u.Id).HasColumnType("int");
        modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles", "User").HasKey(r => new { r.RoleId, r.UserId }).Property(r => r.UserId).HasColumnType("int");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins", "User").HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey }).Property(l => l.UserId).HasColumnType("int"); 
        modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims", "User").Property(c => c.UserId).HasColumnType("int"); 
        modelBuilder.Entity<IdentityRole>().ToTable("MyRoles", "User").HasKey(r => r.Id); 
    }
}
}

即使我删除 HasColumnType("int"),我也会收到此错误。

任何帮助将不胜感激。

【问题讨论】:

    标签: ef-code-first entity-framework-6 asp.net-identity


    【解决方案1】:

    我认为映射可能不正确。在定义 ApplicationDbContext 类时,您使用为通用的角色、登录和声明定义的自定义类,但传递用于映射表的基类。对于 ChangePK 示例,以下映射对我有用。让我知道这是否也适合您。映射应该足够简单

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers");
            modelBuilder.Entity<CustomUserRole>().ToTable("MyUserRoles");
            modelBuilder.Entity<CustomUserLogin>().ToTable("MyUserLogins");
            modelBuilder.Entity<CustomUserClaim>().ToTable("MyUserClaims");
            modelBuilder.Entity<CustomRole>().ToTable("MyRoles");
        }
    

    【讨论】:

    • 我按照你的建议做了,现在可以了。耶!非常感谢您抽出宝贵时间提供帮助。
    • 对我来说,我必须删除我的迁移、数据库中的表并尝试这个。奇迹般有效。不知道为什么所有在线可用的示例都使用身份表而不是自定义表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多