【发布时间】: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