I have managed to customize Identity table names and IdentityUser as described on this forum. My problem is that I'm unable to save user accounts thereafter and I get the error below:
Cannot use table App Users for entity type App Users since it is being used for entity type ApplicationUser and there is no relationship between their primary keys.
I have included herein a demo app that can be downloaded from here
After customizing table names, I did add a migration and updated the database, all these operations where successful, I then used the dotnet ef dbcontext scaffold command to import other database tables. After this, I added another migration which was unsuccessful, ef framework throws the error below:
Cannot use table 'AppRole' for entity type 'AppRole' since it is being used for entity type 'IdentityRole' and there is no relationship between their primary keys.
I have reset the migrations, deleted the _migration table and started all over again but can't get it to work. I therefore beseech you to assist me in this regard.
This is a snapshot of DB-Context
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasAnnotation("ProductVersion", "2.2.3-servicing-35854");
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var table = entityType.Relational().TableName;
if (table.StartsWith("AspNet"))
{
entityType.Relational().TableName = table.Replace(table.Substring(0, 6), "App");
}
};
modelBuilder.Entity<AppRoleClaims>(entity =>
{
entity.HasIndex(e => e.RoleId);
entity.Property(e => e.RoleId).IsRequired();
entity.HasOne(d => d.Role)
.WithMany(p => p.AppRoleClaims)
.HasForeignKey(d => d.RoleId);
});
modelBuilder.Entity<AppRoles>(entity =>
{
entity.HasIndex(e => e.NormalizedName)
.HasName("RoleNameIndex")
.IsUnique()
.HasFilter("([NormalizedName] IS NOT NULL)");
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Name).HasMaxLength(256);
entity.Property(e => e.NormalizedName).HasMaxLength(256);
});
modelBuilder.Entity<AppUserClaims>(entity =>
{
entity.HasIndex(e => e.UserId);
entity.Property(e => e.UserId).IsRequired();
entity.HasOne(d => d.User)
.WithMany(p => p.AppUserClaims)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AppUserLogins>(entity =>
{
entity.HasKey(e => new { e.LoginProvider, e.ProviderKey });
entity.HasIndex(e => e.UserId);
entity.Property(e => e.LoginProvider).HasMaxLength(128);
entity.Property(e => e.ProviderKey).HasMaxLength(128);
entity.Property(e => e.UserId).IsRequired();
entity.HasOne(d => d.User)
.WithMany(p => p.AppUserLogins)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AppUserRoles>(entity =>
{
entity.HasKey(e => new { e.UserId, e.RoleId });
entity.HasIndex(e => e.RoleId);
entity.HasOne(d => d.Role)
.WithMany(p => p.AppUserRoles)
.HasForeignKey(d => d.RoleId);
entity.HasOne(d => d.User)
.WithMany(p => p.AppUserRoles)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AppUserTokens>(entity =>
{
entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name });
entity.Property(e => e.LoginProvider).HasMaxLength(128);
entity.Property(e => e.Name).HasMaxLength(128);
entity.HasOne(d => d.User)
.WithMany(p => p.AppUserTokens)
.HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AppUsers>(entity =>
{
entity.HasIndex(e => e.NormalizedEmail)
.HasName("EmailIndex");
entity.HasIndex(e => e.NormalizedUserName)
.HasName("UserNameIndex")
.IsUnique()
.HasFilter("([NormalizedUserName] IS NOT NULL)");
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Email).HasMaxLength(256);
entity.Property(e => e.NormalizedEmail).HasMaxLength(256);
entity.Property(e => e.NormalizedUserName).HasMaxLength(256);
entity.Property(e => e.UserName).HasMaxLength(256);
});
}
class MyDbContext : IdentityDbContext<?, ?, …>
IdentityDbContext<TUser,TRole,TKey,TUserClaim,TUserRole,TUserLogin,TRoleClaim,TUserToken>
and designate all generic type arguments with your custom types inheriting from them.