2

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.

Here is my connection string

Data Source=.\SQLEXPRESS;Initial Catalog=Jemco;Integrated Security=True;

Could it be something with my app.config?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
2
  • Entity framework code first? Look at (or post it here) the line where you initialize the DataContext. Commented Mar 11, 2014 at 2:22
  • Yes, I'm using entity framework code first. I use a Unit Of Work and a Repository to work with the database but there's nothing to do with a connection in there. Maybe I'm not understanding your request. Commented Mar 11, 2014 at 2:27

3 Answers 3

3

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 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
      <parameters>
        <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; Initial Catalog=Jemco;" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
Sign up to request clarification or add additional context in comments.

Comments

1

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

 public class YOUR_DbContext : DbContext{

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

Comments

0

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.