Using Entity Framework Migrations with MS SQL Server

entity-framework-6

Question

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

My configuration for entity framework is this:

<configuration>
  <connectionStrings>
    <add name="Demo.Data.MyContext"
       providerName="System.Data.SqlClient"
       connectionString="Data Source=(local);Initial Catalog=Connector;    
       Integrated Security=True;MultipleActiveResultSets=True" />
  </connectionStrings>
  <configSections>
  <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"/>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

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?

1
0
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.

0
10/4/2016 11:08:30 AM


Related Questions





Related

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