我希望我的应用程序能够使用各种数据库,如MSSQL、MySQL和SQLite。通过更改配置中的连接字符串,连接工作得很好,我成功地应用了数据库特定的配置,如下所示:
public class ServerDbConfiguration : DbConfiguration
{
public ServerDbConfiguration()
{
switch (ConfigurationManager.AppSettings["DatabaseProvider"].ToUpper())
{
case "MYSQL":
SetHistoryContext("MySql.Data.MySqlClient", (conn, schema) => new ServerHistoryContext(conn, schema));
break;
default:
break;
}
}
}现在,我正在寻找一种方法来实现同样的迁移。在对MSSQL数据库运行Add-Migration之后,我得到如下内容:
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Jobs",
c => new
{
id = c.Int(nullable: false, identity: true),
name = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.id);
// ....
}
}但是显然,除了MSSQL之外,dbo.Jobs不可能解析到表名。
如何在一个项目中对不同的数据库进行多个迁移?或者如果我不能,什么是最好的方法来处理这种情况?
发布于 2014-08-05 08:04:06
命令行工具接受一个-configuration参数来指定配置类,并使用配置类的命名空间来发现/添加迁移。我有两个名称空间:
要添加新迁移,请执行以下操作:
Add-Migration -configuration Server.Migrations.MSSQL.Configuration MyNewMigration然后在Server.Migrations.MSSQL命名空间下添加一个新的迁移。不幸的是,它总是存储在Migrations/文件夹中,所以您需要手动移动它。
若要应用迁移,请运行:
Update-Database -configuration Server.Migrations.MSSQL.Configuration还可以通过代码运行迁移,例如:
System.Data.Entity.Migrations.DbMigrationsConfiguration configuration;
switch (ConfigurationManager.AppSettings["DatabaseProvider"].ToUpper())
{
case "MYSQL":
configuration = new Migrations.MySQL.Configuration();
configuration.MigrationsNamespace = "Server.Migrations.MySQL";
break;
case "MSSQL":
configuration = new Migrations.MSSQL.Configuration();
configuration.MigrationsNamespace = "Server.Migrations.MSSQL";
break;
default:
throw new Exception("Invalid DatabaseProvider, please check your config");
}
configuration.ContextType = typeof(Context);
configuration.MigrationsAssembly = configuration.ContextType.Assembly;
var migrator = new System.Data.Entity.Migrations.DbMigrator(configuration);
migrator.Update();https://stackoverflow.com/questions/25122074
复制相似问题