ASP.NET Identity - 将用户 ID 主键默认类型从字符串更改为 int 以及使用自定义表名时出错 [英] ASP.NET Identity - Error when changing User ID Primary Key default type from string to int AND when using custom table names

查看:16
本文介绍了ASP.NET Identity - 将用户 ID 主键默认类型从字符串更改为 int 以及使用自定义表名时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Microsoft.AspNet.Identity 2.0.0-beta1 and Entity Framework 6.1.0-beta1 (released 11 Feb 2014).

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

I'm getting the following error when I try to change the default type of User ID Primary Key from string to int AND when I try to use custom table names (so User.MyUsers instead of dbo.AspNetUsers):

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

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

I can successfully change the default type of User ID PK from string to int OR change the default identity table names but I cannot do both together without hitting this error.

我的解决方案基于:

1:http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx.

2:使用 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"),我也会收到此错误.

I get this error even if I remove HasColumnType("int").

任何帮助将不胜感激.

推荐答案

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

I think the mapping might be incorrect. While defining the ApplicationDbContext class, you are using the custom classes defined for roles, logins and claims for generic but passing the base classes for mapping the tables. For the ChangePK example the following mapping worked for me. Let me know if this works for you too. The mapping should be simple enough

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

这篇关于ASP.NET Identity - 将用户 ID 主键默认类型从字符串更改为 int 以及使用自定义表名时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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