'How to generate right migration on changed table names Asp.NET Core IdentityUser etc
It is possible to change table names of the IdentityUser, IdentityRole,... tables.
See How can I change the table names when using Visual Studio 2013 ASP.NET Identity?
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("MyUsers");
}
But when I create new migration:
dotnet ef migrations add new ApplicationIdentity
The migration is generated for the original names:
migrationBuilder.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
AccessFailedCount = table.Column<int>(nullable: false),
ConcurrencyStamp = table.Column<string>(nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
LockoutEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
PasswordHash = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
SecurityStamp = table.Column<string>(nullable: true),
TwoFactorEnabled = table.Column<bool>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
});
It is possible to change generated migration manually, but is there any trick how to generate migration with correct names?
Solution 1:[1]
You need to add the migration manually:
Add Empty migration file by using the below command
dotnet ef migrations add rename_tables
A new file will add to migration and you need to change it as the below
public partial class rename_tables : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.RenameTable(name: "AspNetRoleClaims", newName: "RoleClaims"); migrationBuilder.RenameTable(name: "AspNetRoles", newName: "Roles"); migrationBuilder.RenameTable(name: "AspNetUserLogins", newName: "UserLogins"); migrationBuilder.RenameTable(name: "AspNetUserRoles", newName: "UserRoles"); migrationBuilder.RenameTable(name: "AspNetUsers", newName: "Users"); migrationBuilder.RenameTable(name: "AspNetUserTokens", newName: "UserTokens"); migrationBuilder.RenameTable(name: "AspNetUserClaims", newName: "UserClaims"); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.RenameTable(name: "RoleClaims", newName: "AspNetRoleClaims"); migrationBuilder.RenameTable(name: "Roles", newName: "AspNetRoles"); migrationBuilder.RenameTable(name: "UserLogins", newName: "AspNetUserLogins"); migrationBuilder.RenameTable(name: "UserRoles", newName: "AspNetUserRoles"); migrationBuilder.RenameTable(name: "Users", newName: "AspNetUsers"); migrationBuilder.RenameTable(name: "UserTokens", newName: "AspNetUserTokens"); migrationBuilder.RenameTable(name: "AspNetUserClaims", newName: "AspNetUserClaims"); } }
Finally you need to apply the migration
dotnet ef database update
- This is will not change the constraints name but you can also added to you migration file
Solution 2:[2]
You can do this on your DbContext:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable(name: "Users");
entity.Property(e => e.Id).HasColumnName("UserId");
});
}
Will create this migration:
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
UserId = table.Column<string>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false),
ConcurrencyStamp = table.Column<string>(nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
LockoutEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
PasswordHash = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
SecurityStamp = table.Column<string>(nullable: true),
TwoFactorEnabled = table.Column<bool>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.UserId);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Osama AbuSitta |
Solution 2 | LuÃs Antunes |