我正在使用Entity Framework在Windows窗体中为每个租户应用开发一个多租户数据库。我为这样的新租户创建了一个新的数据库:
var ctx = new VentasDBContext("data source=DESKTOP-JSIT42C\\SQLEXPRESS; initial catalog=VentasDBContext; integrated security=SSPI");
Models.Helpers.DatabaseOperations.MigrateDatabase(ctx);
public static void MigrateDatabase(VentasDBContext ctx)
{
var newDbConnString = "data source=DESKTOP-JSIT42C\\SQLEXPRESS; initial catalog=Test; integrated security=SSPI";//context.Database.Connection.ConnectionString;
var connStringBuilder = new SqlConnectionStringBuilder(newDbConnString);
//var newDbName = connStringBuilder.InitialCatalog;
connStringBuilder.InitialCatalog = "Test";
ConfigurationVentas config = new ConfigurationVentas();
DbMigrator migrator = new DbMigrator(config);
//VentasDBContext ctx = new VentasDBContext();
ctx.Database.Create();
Console.WriteLine("Past migrations:");
foreach (string s in migrator.GetDatabaseMigrations())
Console.WriteLine(s);
Console.WriteLine("Local migrations:");
foreach (string s in migrator.GetLocalMigrations())
Console.WriteLine(s);
Console.WriteLine("Pending migrations:");
foreach (string s in migrator.GetPendingMigrations())
Console.WriteLine(s);
Console.WriteLine("Migrating...");
foreach (string s in migrator.GetLocalMigrations())//GetPendingMigrations())
{
//Act
Console.WriteLine("Applying migration {0}", s);
Action act = () => migrator.Update(s);
//Assert
//act.ShouldNotThrow();
}
}
我正在创建这样的上下文:
public VentasDBContext(string connectionString) : base(connectionString)
{
var migrationConfig = new MigrationConf();
Database.SetInitializer<VentasDBContext>(
new MigrateDatabaseToLatestVersion<
VentasDBContext, MigrationConf>(true, migrationConfig));
}
现在有效。 问题是,然后,我创建一个Update-Database,然后再次运行所有迁移。当我尝试查询数据库时,我收到以下错误:
System.Data.SqlClient.SqlException:'已经有一个名为的对象 数据库中的“AuditEntries”。
就像它试图再次创建所有表格一样。
我很抱歉这个长度,我会感激任何帮助!