5

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.

I have the connection strings in the web.config file. 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.

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

3
  • How are you registering the two DbContexts in your Startup file? Also, are you wanting both databases to update when data is changed? Commented Jul 22, 2017 at 7:02
  • @KidCode I have not registred any database in global.asax. when I had only one database, I guess it was not necessary. Commented Jul 22, 2017 at 7:16
  • @KidCode I want to update the database schema like when I do update-database or add-migration but only one db is getting updated. Commented Jul 23, 2017 at 7:23

1 Answer 1

4

Try to enable migrations for second context, use ContextTypeName parameter

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
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.