Using Entity Framework Migrations with MS SQL Server



I am trying to use Entity Framework Migrations to use a local (default) instance of SQL Server.

My configuration for entity framework is this:

    <add name="Demo.Data.MyContext"
       connectionString="Data Source=(local);Initial Catalog=Connector;    
       Integrated Security=True;MultipleActiveResultSets=True" />
  <section name="entityFramework" 
        type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

The context class is this:

namespace Demo.Data
  public class MyContext : DbContext

As far as I read the documentation, a name of a connection string with the same name as the context with make it discoverable by EF.

But when I execute the command:

PM> Update-Database -verbose

The result is this:

Using StartUp project 'Demo'.
Using NuGet project 'Demo.Data'.
Target database is: 'Demo.Data.MyContext' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention).

The question is - why does it insist on targeting .\SQLEXPRESS?

How should I configure my app.config to have EF point to my local SQL Server?

9/10/2016 8:20:25 PM

Popular Answer

Well, apparently there is a trick to get it to work using the PM console:

The project containing the Entity Framework migrations package needs to be selected as "startup project" in Visual Studio. It is not enough just to select the project as "Default project" in PM console.

You can also set this using the

PM> Update-Database -StartUpProjectName "Demo.Data"

switch in the console.

10/4/2016 11:08:30 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow