SlowCheetah不能为EntityFramework迁移进行转换

5
我正在使用EF Code-First Migrations:https://msdn.microsoft.com/en-us/data/jj591621.aspx。在VS的Package Manager Console中使用add-migrationupdate-database等命令。
我还在使用SlowCheetah来管理我们拥有的不同环境,特别是管理每个开发人员都有自己的数据库副本,以便他们可以更新模型更改而不会将其他人锁定在数据库中。因此,一个变化的配置值是目标数据库名称。
(由于某些原因,我们没有像这样使用connectionStrings.config设置,而是将DB名称放在appSettings块中)
包含模型和迁移的项目是TheCustomer.Database项目。
如果我手动更改TheCustomer.Database/app.config中的appSetting值并运行迁移,则会正确使用配置值 - 因此,配置基本上是有效的。
但是,如果我设置SlowCheetah转换以修改给定构建配置的值,选择该配置,重建代码,然后运行迁移,则不应用转换;它使用基本app.config中的值,而不是app.SelectBuildConfig.config
总体而言,SlowCheetah工作正常 - 当我运行sln以获取网站时,它使用由VS Build Config确定的正确Db。
有什么想法吗?
编辑:
ConfigFile:
<?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>

转换功能:
<?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>

除非您向我们展示XDT,否则我们无法真正帮助您。 - code4life
找到了这个链接http://www.petercallaghan.com/2015/04/slow-cheetah-not-transforming-config-files/,但那不是问题所在。 - Brondahl
@SergRogovtsev 请看第一段。从Pack. Mgr. Cons.运行update-database - Brondahl
它对SlowCheetah一无所知,因此它永远不会使用该转换。好消息是,您可以手动将连接字符串传递给“Update-Database”。 - Sergei Rogovtcev
没错,它正在运行C#代码,因此在某个时候必须要构建项目。而且该项目知道应该应用SC变换,对吧?难道不是这样工作的吗? :( - Brondahl
显示剩余2条评论
1个回答

1
@Serg-Rogovtsev是正确的,EF不知道SlowCheetah。在搜索ef core的源代码后,似乎没有办法让EF考虑到已转换的.config文件,特别是当你的DbContext在启动项目之外的项目中声明时,更是如此,尤其是在使用依赖注入时。
因此,还有两种方法可供选择:
  1. 显式定义连接字符串,例如update-database -ConnectionString "Server=(localdb)\MSSQLLocalDB;Database=MyDb;Integrated Security=True;" -ConnectionProviderName System.Data.SqlClient
  2. 定义根app.config文件,使默认(无变换)可与EF迁移一起使用,并确保你的变换对于其余构建是正确的,但很明显这不会起作用,因为你的开发人员正在使用不同的数据库...因此可能使用相同的数据库名称,但使用localdb或多个服务器,并使用DNS来为各自的开发人员重定向
我有一个比较复杂的方法,涉及多个未链接的项目、依赖注入和EF,直到我引入SC变换之前都没有问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接