Entity Framework 6在哪里获取迁移的连接字符串?

jak*_*don 5 entity-framework visual-studio ef-code-first entity-framework-6

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

<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();
Run Code Online (Sandbox Code Playgroud)

现在启动调试器并在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中设置一个连接字符串-但如果这是预期的行为,我很高兴不这样做。期待听到解释!

Dav*_*Dev 2

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

就我而言,我设置了一个控制台应用程序来执行一些批处理工作,在通过种子进行调试时,我注意到与您指出的相同的行为。交换启动项目会更改活动配置。