Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publishing with Multiple Context in Same Database

I encountered a problem with multiple context in EF 6. Recently i had splitted my context into three parts and configured them as had been told here

Everything was fine, until i decided to publish via Visual Studio; because publish wizard detected only one of my context instead of three. And interestingly everytime it detects same context, i couldn't find why, neither first letter of name nor any difference from the others seem cause this.

But i couldn't publish my MVC project because of this. I have to migrate all three contexts while publishing.

After some search, i saw Update-Database command gets connectionstring parameter. This is my last option, if there isn't any way to solve publish wizard i try to update database with this code.

like image 287
bahadir arslan Avatar asked Apr 18 '26 18:04

bahadir arslan


2 Answers

I haven't been able to reproduce this issue. Here are the steps I used (using Visual Studio 2013 Update 2).

Create a new MVC application. Add the following models to the project (two separate Code First models/contexts).

public class CustomerContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}

public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
}

public class ProductContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

Then enable migrations, add a migration and update the local database for both contexts, using the following commands.

 Enable-Migrations -ContextTypeName CustomerContext -MigrationsDirectory Migrations\Customer
 Enable-Migrations -ContextTypeName ProductContext -MigrationsDirectory Migrations\Product
 Add-Migration FirstMigration -ConfigurationTypeName MyWebApp.Migrations.Customer.Configuration
 Add-Migration FirstMigration -ConfigurationTypeName MyWebApp.Migrations.Product.Configuration
 Update-Database -ConfigurationTypeName MyWebApp.Migrations.Customer.Configuration
 Update-Database -ConfigurationTypeName MyWebApp.Migrations.Product.Configuration

Then when I right-click -> Publish the project I get the option to enable migrations on App_Start for both of my contexts (and the ASP.NET Identity context too). If I understand correctly, you are not seeing your additional context(s) in this screen.

Screenshot of publish screen

like image 93
Rowan Miller Avatar answered Apr 21 '26 14:04

Rowan Miller


I've seen this happen when multiple DbContexts share a common connection string. By this I mean:

public class Context1: DbContext
{
 public Context1()
   : this("DefaultConnection")
 {}
 public Context1: (string connectionString)
   : base(connectionString)
 {}

 ....
}

public class Context2: DbContext
{
 public Context2()
   : this("DefaultConnection")
 {}
 public Context2: (string connectionString) 
   : base(connectionString)
 {}

 ...
}

When you Publish, only one DbContext will show up under Settings > Databases. If you change "DefaultConnection" to something else then you will see the distinct DbContexts. Like this:

public class Context1: DbContext
{
 public Context1()
   : this("DefaultConnection")
 {}
 public Context1: (string connectionString)
   : base(connectionString)
 {}

 ....
}

public class Context2: DbContext
{
 public Context2()
   : this("DefaultConnection2")
 {}
 public Context2: (string connectionString) 
   : base(connectionString)
 {}

 ...
}

Maybe this explains the behavior you are seeing.

like image 33
Caleb Van Dyke Avatar answered Apr 21 '26 16:04

Caleb Van Dyke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!