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>
@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:
update-database -ConnectionString "Server=(localdb)\MSSQLLocalDB;Database=MyDb;Integrated Security=True;" -ConnectionProviderName System.Data.SqlClient
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With