Rei*_*l-- 5 c# entity-framework slowcheetah
我使用EF代码优先迁移:
https://msdn.microsoft.com/en-us/data/jj591621.aspx
add-migration,update-database等全部由包管理器控制台VS.叫
我还使用 SlowCheetah 来管理我们拥有的各种环境,特别是包括管理每个开发人员都有自己的数据库副本这一事实,以便他们可以更新他们的模型更改,而无需将其他人锁定在数据库之外。因此,更改的配置值之一是目标数据库名称。
(出于我不会介绍的原因,我们不使用connectionStrings .config此类设置,而是将 DB 名称放在appSettings块中)
我的包含模型和迁移的TheCustomer.Database项目是项目。
如果我手动更改 中的appSetting值TheCustomer.Database/app.config并运行迁移,它会正确使用配置值 - 所以配置基本上是有效的。
但是如果我设置一个 SlowCheetah 转换来修改给定构建配置的值,选择该配置,重建代码,然后运行迁移,那么它不会应用转换;它使用 base 中的值app.config,而不是app.SelectBuildConfig.config
SlowCheetah 总体上运行良好 - 当我运行 sln 来获取网站时,它使用了由 VS Build Config 确定的正确 Db。
有什么想法吗?
编辑:
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<appSettings>
<add key="DbName" value="This Default Value shouldn't work - you should be using a value for a specific build" />
<add key="DbPassword" value="SomePassword" />
</appSettings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup></configuration>
Run Code Online (Sandbox Code Playgroud)
和变换
<?xml version="1.0" encoding="utf-8" ?>
<!-- For more information on using transformations
see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="DbName" value="LocalDev_MDM" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
@Serg-Rogovtsev 是对的,EF 不了解 SlowCheetah。在搜索了ef core的源代码后,似乎没有办法让 EF 考虑转换后的 .config,特别是当您DbContext在与启动项目分开的项目中声明时,并且在使用依赖项注入时更是如此。
那么剩下两种方法:
update-database -ConnectionString "Server=(localdb)\MSSQLLocalDB;Database=MyDb;Integrated Security=True;" -ConnectionProviderName System.Data.SqlClient我有一个有点复杂的方法,涉及多个未链接的项目、依赖项注入和 EF,在我引入 SC 转换之前,该方法一直没有问题。