c# - 如何在 Entity Framework 中重命名迁移表?

标签 c# asp.net .net asp.net-mvc entity-framework

我想将迁移表重命名为 dbo.__MigrationHistorySYSTEM.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();
        }
    }

提前致谢

问候

最佳答案

重命名迁移表的步骤

  1. 创建类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..

    详细信息: Customizing the migrations history table

    关于c# - 如何在 Entity Framework 中重命名迁移表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56335021/

    相关文章:

    .net - 测试设置中的 MsTest DeploymentItem OutputDirectory

    asp.net - 是否可以在 ASP.NET 重定向期间设置 cookie?

    c# - ASP.NET MVC : seperatation of concern for normal/admin user

    c# - 我对 Parallel.ForEach 做错了什么

    C#线程内存异常

    c# - 根据内容自动调整 GridView 列的宽度

    .net - 使用不同的.Net 语言?

    c# - "A generic error occurred in GDI "在 outlook 插件中

    c# - 删除 List<T> 中的备用元素

    c# - 将 UWP App 连接到远程 SQL Server 2008 提供程序 : TCP Provider, 错误:0