Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SlowCheetah not transforming for EntityFramework Migrations

I'm using EF Code-First Migrations: https://msdn.microsoft.com/en-us/data/jj591621.aspx add-migration, update-database, etc. all called from the Package Manager Console in VS.

I'm also using SlowCheetah to manage the various environments we have, notably including managing the fact that each developer has their own copy of the database so that they can update their model changes without locking everyone else out of the DB. So one of the config values that changes is the target DB name.

(For reasons that I won't go into we're not using connectionStrings .config settings as such, but rather having the DB names in appSettings blocks)

My project that contains the models and migrations is the TheCustomer.Database project.

If I manually change the appSetting value in TheCustomer.Database/app.config, and run migrations it uses the config value correctly - so the config is fundamentally working.

But if I set up a SlowCheetah transform to modify the value for a given build config, select that config, rebuild the code and then run the migrations then it doesn't apply the transform; it uses the value in base app.config, not the app.SelectBuildConfig.config

SlowCheetah is working fine in general - when I run the sln to get a website it's using the correct Db as determined by the VS Build Config.

Any thoughts?

EDIT:

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>

And the transform

<?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>
like image 490
Brondahl Avatar asked Nov 09 '22 04:11

Brondahl


1 Answers

@Serg-Rogovtsev is right that EF doesn't know about SlowCheetah. Having searched through the source for ef core it appears there is no way to make EF consider the transformed .config, especially when your DbContext is declared in a project separate to the startup project, and more so when using dependency injection.

There are then two approaches that remain:

  1. explictly define the connection string such as update-database -ConnectionString "Server=(localdb)\MSSQLLocalDB;Database=MyDb;Integrated Security=True;" -ConnectionProviderName System.Data.SqlClient
  2. define your root app.config file such that the default (no transformations) is usable with EF migrations, and make sure your transforms are right for the remaining builds, which evidently won't work as your devs are using different DBs... so perhaps use the same DB name but using localdb or multiple servers and use DNS to redirect for the individual devs

I have a somewhat complicated approach with multiple not linked projects, dependency injection, and EF which was working without issue until I introduced SC transforms.

like image 196
strongbutgood Avatar answered Nov 14 '22 23:11

strongbutgood