Reputation: 1205
I would like rename the migrations table from dbo.__MigrationHistory
to SYSTEM.MigrationsHistory
. How can I do it?
I need inherit from IdentityDbContext
not from HistoryContext
for this reason I can't use the code: modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "SYSTEM")
I attach the code that I am using:
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();
}
}
Thanks in advance
Regards
Upvotes: 2
Views: 1408
Reputation: 1205
Steps for rename migrations table
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));
}
}
}
For create migrations
folder and configuration
class:
enable-migrations
For add migration first_migration
add-migration <migration_name>
For apply migration(s) in data base
update-database
Credits: Ivan Stoev
Before you start you need to know that you can customize the migrations history table only before you apply the first migration..
Detailed information: Customizing the migrations history table
Upvotes: 2