bahadir arslan
bahadir arslan

Reputation: 4762

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.

Upvotes: 3

Views: 2390

Answers (4)

gurbi
gurbi

Reputation: 163

When you want to see all contexts in the Publish dialog, you need to add another connection strings to web.config. They should have different name and be referenced from your context (name in constructor)

Upvotes: 0

Cindy K.
Cindy K.

Reputation: 128

If both dbContexts are using the same database (and therefore the same database connection string in web.config), how do we get Web Deploy to show them both? Do I have to create a separate (duplicate) connection string that points to the same database just to get it to show up as a separate context in the wizard?

Upvotes: 0

Caleb Van Dyke
Caleb Van Dyke

Reputation: 66

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.

Upvotes: 2

Rowan Miller
Rowan Miller

Reputation: 2090

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

Upvotes: 2

Related Questions