I am on a project where we are using Code First on Entity Framework for our database.
We want to change all of our continuous integration to consume a generated MSI package downstream, but with EF that presents a few complications.
I have tried various things from the web, but most seem to require AutomaticMigrations
to be set to true
as well as AutomaticMigrationDataLossAllowed
(see: http://romiller.com/2012/02/09/running-scripting-migrations-from-code/).
I have tried to replicate what Add-Migration
does by looking through .NET reflector but I can't seem to find a way of invoking the command System.Data.Entity.Migrations.AddMigrationCommand
that is called through Powershell.
Anyone have any ideas at all on how I can get close to achieving this without doing something extremely messy? It's something I think a lot of people will want to do/have done...
Many thanks in advance!
Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.
First you need to create a migration. Then in the generated migration file you can write your SQL.
First of all, no way to run Nuget powershell outside visual studio (it uses DTE). Also, everything you write without Visual Studio need to be inserted in the csproj manually (but not an hard task).
Just to show how it works I send you some line of codes. To test them, create MyDll dll (a project test with context and entities) and then manually enabled migrations on MyDll with Enable-Migrations (just to create Configuration.cs).
After that you can use this piece of code to generate the source code
DbConnectionInfo connectionStringInfo = new DbConnectionInfo(
"Server=.;Database=MigrationTest;User=sa;Password=dacambiare", "System.Data.SqlClient"); // We shoud retrieve this from App.config
ToolingFacade toolingFacade = new ToolingFacade(
"MyDll", // MigrationAssemblyName. In this case dll should be located in "C:\\Temp\\MigrationTest" dir
"MyDll", // ContextAssemblyName. Same as above
null,
"C:\\Temp\\MigrationTest", // Where the dlls are located
"C:\\Temp\\MigrationTest\\App.config", // Insert the right directory and change with Web.config if required
"C:\\Temp\\App_Data",
connectionStringInfo)
{
LogInfoDelegate = s => {Console.WriteLine(s);},
LogWarningDelegate = s => { Console.WriteLine("WARNING: " + s); },
LogVerboseDelegate = s => { Console.WriteLine("VERBOSE: " + s); }
};
ScaffoldedMigration scaffoldedMigration = toolingFacade.Scaffold("MyMigName", "C#", "MyAppNameSpace", false);
Console.WriteLine(scaffoldedMigration.DesignerCode);
Console.WriteLine("==================");
Console.WriteLine(scaffoldedMigration.UserCode);
// Don't forget the resource file that is in the scaffoldedMigration
EDIT
I forgot the namespaces and are not used often so here you are
using System;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.Design;
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