Publishing with Multiple Context in Same Database

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

Question

In EF 6, I ran into a bug with various context. I recently divided my context into three sections and set them up as instructed (here).

Everything worked well up until the point where I wanted to publish using Visual Studio, as the publish wizard only identified one context out of three. And intriguingly, it always identifies the same context. I was unable to determine why; neither the initial letter of the name nor any distinction from the others appeared to be the cause.

But because of this, I was unable to publish my MVC app. While publishing, I must move all three contexts.

I discovered the Update-Database call takes a connectionstring parameter after some searching. This is my final resort; if the publish wizard cannot be fixed, I will attempt to update the database using this code.

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

Popular Answer

This has happened in my experience when several DbContexts share a single connection string. This is to say:

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)
 {}

 ...
}

Only one DbContext will appear under Settings > Databases when you publish. You can observe the various DbContexts by changing "DefaultConnection" to something different. akin to 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)
 {}

 ...
}

This could perhaps explain the behavior you are observing.

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