Entity Framework 两个不同数据库的代码优先迁移
Entity Framework code first migrations for two different databases
我对这里的情况很困惑。我需要连接到两个单独的数据库,一个是 SQL 服务器数据库,另一个是 MySQL 数据库。
我在 web.config
文件中有连接字符串。我能够连接到服务器并访问数据。
但是,我需要 运行 同时在两台服务器上进行实体迁移。或者一个接一个,我觉得不可能。
这是我的数据库上下文:
// Database 1
public class DatabaseContext : DbContext
{
public DatabaseContext() : base("name=OldDBContext"){ }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { }
public static DatabaseContext Create()
{
return new DatabaseContext();
}
public DbSet<User> UserModel { get; set; }
}
// Database 2
public class NewDatabaseContext : DbContext
{
public NewDatabaseContext() : base("name=NewDBContext") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { }
public static NewDatabaseContext Create()
{
return new NewDatabaseContext();
}
public DbSet<UserData> UserDataModel { get; set; }
}
最初我只有一个数据库,我过去常常在程序包管理器控制台中 运行 add-migration MigrationName
,它会创建一个包含数据库更改的迁移。
但是现在,当我有两个单独的数据库时,迁移不包含第二个数据库或我后来添加的数据库的任何更改。
请帮忙。非常感谢任何帮助。
谢谢
尝试为第二个上下文启用迁移,使用 ContextTypeName 参数
Enable-Migrations -EnableAutomaticMigrations -ContextTypeName
NamespaceOfContext.NewDatabaseContext
它将创建单独的配置。如果在 Migrations 文件夹中发生命名冲突重命名配置文件,那么您可以 运行 数据库更新特定配置
Update-Database -ConfigurationTypeName ConfigurationName
我对这里的情况很困惑。我需要连接到两个单独的数据库,一个是 SQL 服务器数据库,另一个是 MySQL 数据库。
我在 web.config
文件中有连接字符串。我能够连接到服务器并访问数据。
但是,我需要 运行 同时在两台服务器上进行实体迁移。或者一个接一个,我觉得不可能。
这是我的数据库上下文:
// Database 1
public class DatabaseContext : DbContext
{
public DatabaseContext() : base("name=OldDBContext"){ }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { }
public static DatabaseContext Create()
{
return new DatabaseContext();
}
public DbSet<User> UserModel { get; set; }
}
// Database 2
public class NewDatabaseContext : DbContext
{
public NewDatabaseContext() : base("name=NewDBContext") { }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { }
public static NewDatabaseContext Create()
{
return new NewDatabaseContext();
}
public DbSet<UserData> UserDataModel { get; set; }
}
最初我只有一个数据库,我过去常常在程序包管理器控制台中 运行 add-migration MigrationName
,它会创建一个包含数据库更改的迁移。
但是现在,当我有两个单独的数据库时,迁移不包含第二个数据库或我后来添加的数据库的任何更改。
请帮忙。非常感谢任何帮助。
谢谢
尝试为第二个上下文启用迁移,使用 ContextTypeName 参数
Enable-Migrations -EnableAutomaticMigrations -ContextTypeName
NamespaceOfContext.NewDatabaseContext
它将创建单独的配置。如果在 Migrations 文件夹中发生命名冲突重命名配置文件,那么您可以 运行 数据库更新特定配置
Update-Database -ConfigurationTypeName ConfigurationName