在下列代码片段中已经EF6迁移表的表名更改为
MigrationHistory
注意:下代码片段建议和DbContext同程序集
1 public class ModelConfiguration : DbConfiguration 2 { 3 public ModelConfiguration() 4 { 5 6 var conn = System.Configuration.ConfigurationManager.ConnectionStrings[ConnectionConst.DefaultConnectionKey]; 7 var connProvder = conn.ProviderName; 8 9 this.SetHistoryContext(connProvder, (connection, defaultSchema) => new MigrationContext(connection, defaultSchema)); 10 } 11 }
1 /// <summary> 2 /// 迁移表 3 /// </summary> 4 public class MigrationContext : HistoryContext 5 { 6 public MigrationContext(DbConnection dbConnection, string defaultSchema) 7 : base(dbConnection, defaultSchema) 8 { 9 } 10 11 protected override void OnModelCreating(DbModelBuilder modelBuilder) 12 { 13 base.OnModelCreating(modelBuilder); 14 modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory"); 15 } 16 }
1 public class ConnectionConst 2 { 3 /// <summary> 4 /// 数据库连接默认字符串 5 /// </summary> 6 public const string DefaultConnectionKey = "DefaultConnection"; 7 }