简体   繁体   English

EF迁移不使用Web Config连接字符串

[英]EF Migrations not using Web Config connection string

I have my web.config file: 我有我的web.config文件:

<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 : 我有我的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 我有我的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? 我是否在正确配置Web?

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. 如果以上方法均无济于事,您可以尝试右键单击Web应用程序并选择“发布”,然后在“调试”构建配置中进行简单的文件系统发布,然后手动检查web.config以查看其是否正确转换,甚至将其扔到wwwroot中,如果存在连接字符串问题,则应获得一个例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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