数据库
首页 > 数据库> > c# – 实体框架代码首次迁移两个不同的数据库

c# – 实体框架代码首次迁移两个不同的数据库

作者:互联网

我对这里的情况很困惑.我需要连接到两个单独的数据库,一个是SQL Server数据库,另一个是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 

它将创建单独的配置.如果发生命名冲突,请在“迁移”文件夹中重命名配置文件,然后可以针对特定配置运行数据库更新

Update-Database -ConfigurationTypeName ConfigurationName

标签:c,entity-framework,ef-code-first,entity-framework-6,ef-migrations
来源: https://codeday.me/bug/20190627/1306696.html