【问题标题】:Where Does Entity Framework 6 Get Connection String For Migrations?Entity Framework 6 在哪里获取迁移的连接字符串?
【发布时间】:2018-01-31 02:46:35
【问题描述】:

我首先使用 EntityFramework 6 代码进行迁移。我有两个项目——UI 和 BusinessLogic。 UI 是一个依赖于 BusinessLogic 的 .NET MVC Web 应用程序,BusinessLogic 是一个类库。 WebUI 的配置部分包括连接字符串。 BusinessLogic 还具有以下配置部分(未提供连接字符串)并包含实际的代码优先迁移类:

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

在包管理器控制台中,当我运行 update-database -ProjectName BusinessLogic 时,它以某种方式知道要使用我的 UI 项目中的连接字符串。

为了了解发生这种情况的方式/原因,我将调试器附加到我的 DbContext 类的构造函数中,如下所示:

public MyDbContext() : base(CloudConfigurationManager.GetSetting(CONNECTION_STRING_KEY, false))
{
  //--launch the debugger
  System.Diagnostics.Debugger.Launch();

现在启动调试器并在 DbContext 构造函数中设置断点,我可以评估 AppDomain.CurrentDomain.BaseDirectory 产生:

C:\\someRepoLocation\\Source\\BusinessLogic\\bin\\Debug\\ 这似乎表明当前的应用程序域是 BusinessLogic。

但是,评估 AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 会产生: C:\\someRepoLocation\\Source\\UI\\tmpD442.tmp

所以当前的应用程序域是 BusinessLogic,但它使用的是根 UI 文件夹中的一些 .tmp 配置文件?瓦?

如果没有在 BusinessLogic 的 app.config 中提供连接字符串(存在迁移),任何人都可以解释它是如何/为什么工作的吗?我原以为我必须设置一个连接字符串在 BusinessLogic 类库的 app.config 中——但如果这是预期的行为,我很高兴不这样做。期待听到什么解释!

【问题讨论】:

    标签: visual-studio entity-framework entity-framework-6 ef-code-first


    【解决方案1】:

    通过我自己的一些测试,我发现update-database调用期间使用的配置是当前设置的启动项目。

    在我的例子中,我设置了一个控制台应用程序来执行一些批处理工作,并且在通过种子进行调试时,我注意到了与您提到的相同的行为。交换启动项目是更改活动配置的原因。

    【讨论】:

      猜你喜欢
      • 2018-05-08
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 2022-06-15
      相关资源
      最近更新 更多