Skip to content Skip to sidebar Skip to footer

Changed Connection String But Mvc Application Still Using Localdb

I changed my connection string in the web.config to use sqlexpress rather than just localdb. My app still creates the database in localdb rather than in sqlexpress. I'm stumped. He

Solution 1:

You should be using <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> instead of the LocalDbConnectionFactory and also adding the connection string amongst its <parameters> tags.

<?xml version="1.0" encoding="utf-8"?><configuration><configSections><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><sectionname="entityFramework"type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"requirePermission="false" /></configSections><entityFramework><defaultConnectionFactorytype="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"><parameters><parametervalue="Data Source=.\SQLEXPRESS; Integrated Security=True; Initial Catalog=Jemco;" /></parameters></defaultConnectionFactory><providers><providerinvariantName="System.Data.SqlClient"type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /></providers></entityFramework></configuration>

Solution 2:

The < defaultConnectionFactory > element settings only apply when you have supplied no 'hints' to Entity Framework.

You can override the DbContext base class constructor to supply 'hints' to Entity Framework. Assuming you are defining your connection string in the .config file all you need to do is have your YOUR_DbContext class call the base DbContext(string) constructor supplying your connection string name, add the below public constructor to your DbContext class

publicclassYOUR_DbContext : DbContext{

    publicYOUR_DbContext()
        : base("**YOUR_ConnectionStringNameHere**"){//Optional - Write the actual connect string out to the Output window 
        Debug.WriteLine(Database.Connection.ConnectionString);
    }

Solution 3:

One other reasons for this could be when you have several projects on a solution and you chose a project as start up project other than the project your DBContext is and you did not set the connection string in the start up project.Just set the connection string in the start up project as well.

Hope help someone ;)

Post a Comment for "Changed Connection String But Mvc Application Still Using Localdb"