简体   繁体   中英

EF Migrations not using Web Config connection string

I have my web.config file:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SRV;Initial Catalog=SomeCatalog;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

I have my web.debug.config :

<connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=.\SRV;Initial Catalog=SomeCatalog;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
            xdt:Transform="SetAttributes" xdt:Locator="Match(DefaultConnection)"/>
  </connectionStrings>

I have my web.defaultserver.config

<connectionStrings>
   <add name="DefaultConnection" connectionString="Data Source=.\;Initial Catalog=EverybodyPilates;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
      xdt:Transform="SetAttributes" xdt:Locator="Match(DefaultConnection)" />
  </connectionStrings>

When I run the migrations:

> Update-Database -Force -Verbose

It hangs for ages because it can't seem to find my connection string. Am I doing my web config correctly?

Or have I got something else incorrect?

It's interesting that it hangs rather than returning an error. By default it will pull connection information from the startup project. You can specify the projects manually:

Update-Database –SourceMigration $InitialDatabase -ProjectName ProjectWithMigrations -StartUpProjectName WebApp

You can also force your context to use that connection string, by default it will look for one with based on the context name.

public class MyContext : DbContext
{
    static MyContext()
    {

    }

    public MyContext()
        : base("Name=DefaultConnection")
    {

    }
}

If none of that seems to help you might try right clicking on your web app and choosing publish, and do a simple file system publish in Debug build configuration and manually inspect the web.config to see if it transformed properly and even throw it in wwwroot, if there are connection string issues you should get an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM