anayarojo
anayarojo

Reputation: 1205

How to rename migrations table in entity framework?

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 HistoryContextfor 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

Answers (1)

anayarojo
anayarojo

Reputation: 1205

Steps for rename migrations table

  1. Create class 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");
            }
        }
    }

  1. Create class ModelConfiguration
    using System.Data.Entity;

    namespace CustomizableMigrationsHistoryTableSample
    {
        public class ModelConfiguration : DbConfiguration
        {
            public ModelConfiguration()
            {
                this.SetHistoryContext("System.Data.SqlClient",
                    (connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema));
            }
        }
    }
  1. If you have existing migrations, you will need to delete migrations and database for execute the next commands:

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

Related Questions