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 (12)

  1. More a question than a comment, but how would you change this for Oracle 11g?

  2. Rich says:

    Thanks. Helped me get deployed.

  3. Brian says:

    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.

  4. John Ortiz Ordoñez (InfZero) says:

    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.

  5. Will W says:

    Thanks a bunch. This was very confusing.

  6. Big Thanks - says:

    Great tip! simple and useful!

  7. Ryan says:

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

  8. Metin says:

    Thank you; 🙂

  9. Good Stuff says:

    It's like you read my mind simply by querying for defaultConnectionFactory. Much obliged for your post!

Skip to main content