Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 701
DavidM Avatar asked Mar 09 '14 16:03

DavidM


People also ask

How can I change the table names when using ASP NET identity?

You can do this easily by modifying the IdentityModel. cs as per the below: Override OnModelCreating in your DbContext then add the following, this will change AspNetUser table to "Users" you can also change the field names the default Id column will become User_Id.

What is Aspnet identity?

ASP.NET Identity is Microsoft's user management library for ASP.NET. It includes functionality such as password hashing, password validation, user storage, and claims management. It usually also comes with some basic authentication, bringing its own cookies and multi-factor authentication to the party.


1 Answers

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");
    }
like image 165
Suhas Joshi Avatar answered Sep 21 '22 22:09

Suhas Joshi