简体   繁体   English

实体框架代码首先迁移两个不同的数据库

[英]Entity Framework code first migrations for two different databases

I am quite confused with a situation here. 我对这里的情况很困惑。 I need to connect to two separate databases, one is a SQL Server database and the other is a MySQL database. 我需要连接到两个单独的数据库,一个是SQL Server数据库,另一个是MySQL数据库。

I have the connection strings in the web.config file. 我在web.config文件中有连接字符串。 I am able to connect to the servers and access data. 我能够连接到服务器并访问数据。

But, I need to run entity migration on both the servers simultaneously. 但是,我需要同时在两台服务器上运行实体迁移。 Or one by one, which I don't think is possible. 或者一个接一个,我认为这是不可能的。

Here is my database context: 这是我的数据库上下文:

// 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; }
}

Initially I had only one database and I used to run add-migration MigrationName in the package manager console and it would create a migration with the changes in the database. 最初我只有一个数据库,我曾经在包管理器控制台中运行add-migration MigrationName ,它会创建一个包含数据库中更改的迁移。

But now, when I have two separate databases, the migrations does not contain any changes in the second database or the one I added later. 但是现在,当我有两个单独的数据库时,迁移不包含第二个数据库或我稍后添加的数据库中的任何更改。

Please help. 请帮忙。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thank you 谢谢

Try to enable migrations for second context, use ContextTypeName parameter 尝试为第二个上下文启用迁移,使用ContextTypeName参数

Enable-Migrations -EnableAutomaticMigrations -ContextTypeName
NamespaceOfContext.NewDatabaseContext 

It will create separate configuration. 它将创建单独的配置。 If naming conflicts occured rename configuration file in Migrations folder, then you can run database update for specific configuration 如果发生命名冲突,请在“迁移”文件夹中重命名配置文件,然后可以针对特定配置运行数据库更新

Update-Database -ConfigurationTypeName ConfigurationName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM