我想将迁移表重命名为 dbo.__MigrationHistory
至SYSTEM.MigrationsHistory
。我该怎么做?
我需要继承IdentityDbContext
不是来自HistoryContext
因此我无法使用代码:modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "SYSTEM")
我附上我正在使用的代码:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// ACCOUNT
modelBuilder.Entity<ApplicationUser>().ToTable(tableName: "Users", schemaName: "ACCOUNT");
modelBuilder.Entity<ApplicationRole>().ToTable(tableName: "Roles", schemaName: "ACCOUNT");
modelBuilder.Entity<ApplicationUserLogin>().ToTable(tableName: "UserLogins", schemaName: "ACCOUNT");
modelBuilder.Entity<ApplicationUserRole>().ToTable(tableName: "UserRoles", schemaName: "ACCOUNT");
modelBuilder.Entity<ApplicationUserClaim>().ToTable(tableName: "UserClaims", schemaName: "ACCOUNT");
// SYSTEM
// modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "SYSTEM");
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
提前致谢
问候
最佳答案
重命名迁移表的步骤
- 创建类
MyHistoryContext
:
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Migrations.History;
namespace CustomizableMigrationsHistoryTableSample
{
public class MyHistoryContext : HistoryContext
{
public MyHistoryContext(DbConnection dbConnection, string defaultSchema)
: base(dbConnection, defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "SYSTEM");
}
}
}
- 创建类
ModelConfiguration
using System.Data.Entity;
namespace CustomizableMigrationsHistoryTableSample
{
public class ModelConfiguration : DbConfiguration
{
public ModelConfiguration()
{
this.SetHistoryContext("System.Data.SqlClient",
(connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema));
}
}
}
- 如果您有现有迁移,则需要删除迁移和数据库才能执行以下命令:
用于创建migrations
文件夹和configuration
类:
enable-migrations
用于添加迁移first_migration
add-migration <migration_name>
用于在数据库中应用迁移
update-database
鸣谢:Ivan Stoev
Before you start you need to know that you can customize the migrations history table only before you apply the first migration..
关于c# - 如何在 Entity Framework 中重命名迁移表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56335021/