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

asp.net-identity ef-code-first entity-framework-6

Question

I work for Microsoft. AspNet. Entity Framework 6.1.0-beta1 with Identity 2.0.0.0 (released 11 Feb 2014).

I'm trying to use custom table names, such as User.MyUsers instead of dbo.AspNetUsers, but I'm receiving the following problem when I try to change the default type of User ID Primary Key from string to int AND:

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

I can successfully change the default identity table names OR the default User ID PK type from string to int, but I am unable to do both together without running into this problem.

My response is supported by:

1: From http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx, section "Make the kind of Primary Key be extensible for Users and Roles."

2: zzzz-zzzz-zzzz.

Actually, my 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); 
    }
}
}

This problem persists even after I delete HasColumnType ("int").

I would be very grateful for any assistance.

1
7
5/23/2017 12:23:05 PM

Accepted Answer

I believe the mapping might be off. You use the custom classes created for roles, logins, and claims when defining the ApplicationDbContext class, but you pass the base classes to map the tables. I had success with the following mapping for the ChangePK example. If this works for you too, please let me know. The mapping ought to be straightforward.

   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");
    }
10
3/10/2014 7:16:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow