【问题标题】:SlowCheetah not transforming for EntityFramework MigrationsSlowCheetah 没有为 EntityFramework 迁移进行转换
【发布时间】:2015-12-28 07:21:31
【问题描述】:

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

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

(由于我不会详细说明的原因,我们没有使用 connectionStrings .config 设置,而是在 appSettings 块中使用 DB 名称)

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

如果我手动更改 TheCustomer.Database/app.config 中的 appSetting 值,并运行迁移,它会正确使用配置值 - 所以配置基本上可以工作。

但是,如果我设置了一个 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>

【问题讨论】:

标签: c# entity-framework slowcheetah


【解决方案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 转换之前,它一直没有问题。

【讨论】:

    猜你喜欢
    • 2013-11-16
    • 2013-09-03
    • 2012-11-10
    • 2015-02-07
    • 1970-01-01
    • 2014-10-12
    • 2019-11-21
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多