Reputation: 10838
I was following that article to change UserId to int http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity#mvcupdate3
All went good and I had made that change but I want to do someting more, I want to rename tables as well.
In order to do that extnded OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
with
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Accounts").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<ApplicationUser>().ToTable("Accounts").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
modelBuilder.Entity<IdentityRole>().ToTable("Roles");
}
And then when I am generating the migrations (add-migration) I had an error:
Refinery.Web.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Refinery.Web.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
My entities looking like that:
public class ApplicationUser : IdentityUser <int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
[ForeignKey("UserId")]
public virtual List<Option> Options{ get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public CustomUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
public CustomRoleStore(ApplicationDbContext context)
: base(context)
{
}
}
public class Option
{
[Key]
public int Id { get; set; }
public int UserId { get; set; }
public string Title { get; set; }
}
May be some one can help?
I didnt have such issue with renaming Identity tables this way unless I changed the UserId type to int.
Upvotes: 3
Views: 726
Reputation: 4040
The problem is that while you have implemented your custom Identity models, you are still referencing the original identity models in the OnModelCreating
method
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("Accounts").Property(p => p.Id).HasColumnName("UserId");
modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles");
modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims");
modelBuilder.Entity<CustomRole>().ToTable("Roles");
}
Upvotes: 4