Using Entity Framework Migrations with MS SQL Server

entity-framework-6

Question

I'm attempting to utilise the local (default) instance of SQL Server with Entity Framework Migrations.

This is how I have entity framework configured:

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

This is the context class:

namespace Demo.Data
{
  public class MyContext : DbContext
  { 
  }
}

According to what I've read in the documentation, EF will be able to find a connection string if its name is the same as the context.

However, when I carry out 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).

Why does it keep choosing to target.SQLEXPRESS is the question.

How should my app.config be set up such that EF points to my own SQL Server locally?

1
0
9/10/2016 8:20:25 PM

Popular Answer

Evidently, utilising the PM console, there is a method to make it work:

In Visual Studio, the project containing the Entity Framework migrations package must be chosen as the "starting project." Simply choosing the project as the "Default project" in the PM console is insufficient.

Additionally, utilising the

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

On the console, a switch.

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