c# - SlowCheetah 没有为 EntityFramework 迁移进行转换

标签 c# entity-framework slowcheetah

我正在使用 EF 代码优先迁移:
https://msdn.microsoft.com/en-us/data/jj591621.aspxadd-migration , update-database等都是从 VS 中的包管理器控制台调用的。

我还使用 SlowCheetah 来管理我们拥有的各种环境,特别是管理每个开发人员都有自己的数据库副本的事实,这样他们就可以更新他们的模型更改,而不会将其他人锁定在数据库之外。因此,更改的配置值之一是目标数据库名称。

(由于我不会进入的原因,我们没有使用 connectionStrings .config 设置,而是在 appSettings block 中使用数据库名称)

我的包含模型和迁移的项目是 TheCustomer.Database项目。

如果我手动更改 appSetting TheCustomer.Database/app.config 中的值,并运行它正确使用配置值的迁移 - 所以配置从根本上是有效的。

但是,如果我设置了一个 SlowCheetah 转换来修改给定构建配置的值,选择该配置,重新构建代码然后运行迁移,那么它不会应用转换;它使用基数 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>

和变换
<?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>

最佳答案

@Serg-Rogovtsev 是正确的,EF 不知道 SlowCheetah。搜索了 ef core 的来源似乎没有办法让 EF 考虑转换后的 .config,尤其是当您的 DbContext在与启动项目分开的项目中声明,在使用依赖注入(inject)时更是如此。
然后还有两种方法:

  • 显式定义update-database -ConnectionString "Server=(localdb)\MSSQLLocalDB;Database=MyDb;Integrated Security=True;" -ConnectionProviderName System.Data.SqlClient等连接字符串
  • 定义您的根 app.config 文件,以便默认(无转换)可用于 EF 迁移,并确保您的转换适用于剩余的构建,这显然不起作用,因为您的开发人员正在使用不同的数据库......所以可能使用相同的数据库名称,但使用 localdb 或多个服务器,并使用 DNS 为各个开发人员重定向

  • 在我引入 SC 转换之前,我对多个未链接的项目、依赖注入(inject)和 EF 有一个有点复杂的方法。

    关于c# - SlowCheetah 没有为 EntityFramework 迁移进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32867232/

    相关文章:

    c# - Entity Framework - SQL Azure 重试策略

    c# - DbContext 只是我的数据库在内存中的架构或架构和记录

    c# - 如何从 Web.config 获取常量字符串?

    azure - 转换 Web 角色中的外部配置

    c# - 在单元测试中使用 xml 转换

    c# - 如何在数据表的每一列中获取最大字符串长度

    c# - 有人可以解释一下 Child.Measure(availableSize) 在 silverlight 中的作用吗?

    c# - 良好的 C#.NET 解决方案来管理频繁的数据库轮询

    entity-framework - Entity Framework 错误 : The field X is required

    asp.net - 转换没有键或名称属性的配置文件