ASP.NET Identity - Error when changing User ID Primary Key default type from string to int AND when using custom table names

微笑、不失礼 提交于 2020-01-01 05:10:08

问题


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

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):

"The entity types 'IdentityUser' and 'ApplicationUser' cannot share table 'MyUsers' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them."

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.

My solution is based on:

1: Section "Make the type of Primary Key be extensible for Users and Roles" from 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?.

My actual code is:

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

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

Any help would be greatly appreciated.


回答1:


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


来源:https://stackoverflow.com/questions/22284610/asp-net-identity-error-when-changing-user-id-primary-key-default-type-from-str

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!