Publishing with Multiple Context in Same Database

asp.net-mvc entity-framework entity-framework-6 visual-studio-2013 webdeploy

Question

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.

1
2
5/23/2017 11:59:49 AM

Popular Answer

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.

2
8/12/2014 1:45:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow