Search code examples
model-view-controllerconnection-stringcode-firstentity-framework-migrationsn-tier-architecture

Code-First Migrations Always Defaults to Local DB


I've run across and solved this problem a long time ago but forgot how I solved it...

I have an n-tier structure with 4 projects (Models, Data, Service, Web) and can't get Code-First Migrations to use the correct connection string. I have set the connection string in Web.Config and set the Web project as default start project for the solution. But no matter what when I initialize or update Migrations, it creates a new local .mdf default database.


Solution

  • I found the answer finally in another thread... The problem was that my connection string name specified in my DbContext base didn't match the actual name of my connection string in my web.config.

    public class WWJEntities : DbContext
    {
        public WWJEntities() : base("WWJEntities") { }
    

    and

    <add name="WWJ" connectionString="Data Sou...
    

    Once I changed name="WWJ" to name="WWJEntities" it worked as expected. As long as that matches and you've got your connection string in your solution startup project, you're golden.