Reputation: 921
I'm trying to customize my IdentityUser to generate a primary key as integer and change the table names. The thing is: the primary key is generated correctly and all standard tables are created correctly by Migration with the new names, except by the User one. When I apply the Add-Migration Comand it returns me two "user tables".
Here is my code.
My custom Identity classes
public class User : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
}
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; }
}
My ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public ApplicationDbContext() : base("CONNSTRING") { }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().ToTable("Users", "dbo");
modelBuilder.Entity<CustomRole>().ToTable("Role","dbo");
modelBuilder.Entity<CustomUserRole>().ToTable("UserRole", "dbo");
modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogin", "dbo");
modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims", "dbo");
}
}
And the script generated by EF, which you can see generates me two "User tables" : the standard one "AspNetUsers" and my custom one "Users".
public partial class teste : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Role",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.UserRole",
c => new
{
UserId = c.Int(nullable: false),
RoleId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.Role", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.Int(nullable: false, identity: true),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
CreateTable(
"dbo.UserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.Int(nullable: false),
ClaimType = c.String(),
ClaimValue = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.UserLogin",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.Users",
c => new
{
Id = c.Int(nullable: false, identity: true),
Email = c.String(),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropForeignKey("dbo.UserRole", "UserId", "dbo.Users");
DropForeignKey("dbo.UserLogin", "UserId", "dbo.Users");
DropForeignKey("dbo.UserClaims", "UserId", "dbo.Users");
DropForeignKey("dbo.UserRole", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.UserLogin", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.UserClaims", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.UserRole", "RoleId", "dbo.Role");
DropIndex("dbo.UserLogin", new[] { "UserId" });
DropIndex("dbo.UserClaims", new[] { "UserId" });
DropIndex("dbo.AspNetUsers", "UserNameIndex");
DropIndex("dbo.UserRole", new[] { "RoleId" });
DropIndex("dbo.UserRole", new[] { "UserId" });
DropIndex("dbo.Role", "RoleNameIndex");
DropTable("dbo.Users");
DropTable("dbo.UserLogin");
DropTable("dbo.UserClaims");
DropTable("dbo.AspNetUsers");
DropTable("dbo.UserRole");
DropTable("dbo.Role");
}
}
Additional information: The application uses the "AspNetUsers" table as the principal one.
Thankful for any help.
Upvotes: 1
Views: 963
Reputation: 18181
I think you will have to map the IdentityUser to your new user table like this in your OnModelCreating method.
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("UserId");
Upvotes: 0