Partager via


Changing EF's default connection factory from LocalDb to Sql Server

When you create a new project that makes use of Entity Framework 5, you'll notice how it uses LocalDb by default now. But let's assume you have a fully working instance of Sql Server that you wish to work against instead of LocalDb. How do you tell EF to use it? Simple, modify your application's config file. If you are running a web service or a website, your config file is web.config. If you are running a console application or a windows application, look for app.config.

LocalDb

  <entityFramework>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

      <parameters>

        <parameter value="v11.0" />

      </parameters>

    </defaultConnectionFactory>

  </entityFramework>

Now, replace that portion of the configuration to make use of Sql Server instead of LocalDb.

Sql Server

  <entityFramework>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">

      <parameters>

        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />

      </parameters>

    </defaultConnectionFactory>

  </entityFramework>

You only need to change the configuration to match your appropriate connection string.

David.

Comments

  • Anonymous
    March 13, 2013
    More a question than a comment, but how would you change this for Oracle 11g?

  • Anonymous
    April 04, 2013
    Thanks. Helped me get deployed.

  • Anonymous
    November 01, 2013
    Is there a way to turn it off or to ignore the parameters? I don't want to track another Connection String outside of the connectionStrings section.

  • Anonymous
    October 06, 2014
    I'm following the examples of Microsoft ADO.NET Entity Framework Step by Step book, but I was stucked in the example about Code First Workflow. In it there is no any hint about the database server that must be used to automatically créate the database, its tables, and the sample data. ...Then I followed your proposed solution and it worked smoothly. Thanks.

  • Anonymous
    October 12, 2014
    yes, It works

  • Anonymous
    November 10, 2014
    Thanks

  • Anonymous
    November 12, 2014
    Thanks a bunch. This was very confusing.

  • Anonymous
    December 14, 2014
    Thanks; it works

  • Anonymous
    December 20, 2014
    Great tip! simple and useful!

  • Anonymous
    February 16, 2015
    Thanks! I wish there was an automated way to switch data providers in Visual Studio. Maybe there is! Comment. (Note: I know how to connect to a data source with EF, etc.)

  • Anonymous
    March 08, 2015
    Thank you; :)

  • Anonymous
    November 02, 2015
    It's like you read my mind simply by querying for defaultConnectionFactory. Much obliged for your post!