这是我所做的,可能不是那么漂亮,但它适用于我们(注意这是我们本地的开发和 CI 环境):
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator(OracleConnectionInfo.InvariantName, new OracleEntityMigrationSqlGenerator());
}
protected override void Seed(MyContext context)
{
Logger.Write("In Seed method. User: " + userName);
MyInitializer.AddUpdateDataSql(context);
}
}
public class MyInitializer : MigrateDatabaseToLatestVersion<MyContext, Configuration>
{
private const string Schemaname = "MYSCHEMA";
public static void AddUpdateDataSql(Pronova2Context context)
{
DeleteTableData(context);
DropCreateSequences(context);
PopulateTypeTypeData(context);
// etc.
}
private static void DeleteTableData(DbContext context)
{
context.Database.ExecuteSqlCommand(@"delete " + Schemaname + ".T_TABLE1"); }
context.Database.ExecuteSqlCommand(@"delete " + Schemaname + ". T_TABLE2"); }
// etc.
}
private static void DropCreateSequences(DbContext context)
{
context.Database.ExecuteSqlCommand(@"DROP SEQUENCE " + Schemaname + ".T_TABLE1_SEQ"); }
context.Database.ExecuteSqlCommand(@"DROP SEQUENCE " + Schemaname + ".T_TABLE2_SEQ"); }
// etc.
}
private static void PopulateTypeTypeData(DbContext context)
{
try { context.Database.ExecuteSqlCommand(@"Insert into " + Schemaname + ".T_TYPE_TYPE (TYPE_TYPE_ID,NAME,DESCRIPTION) values (1,'TypeType1','Name 1')");
try { context.Database.ExecuteSqlCommand(@"Insert into " + Schemaname + ".T_TYPE_TYPE (TYPE_TYPE_ID,NAME,DESCRIPTION) values (2,'TypeType1','Name 2')");
// etc.
}
}