Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically creating code first migrations

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.

  • When the model changes, we have to generate a Code Based Migration otherwise the package will break (Database vs. Model)
  • We prefer to remove the creation of migrations from the team (based on https://msdn.microsoft.com/en-us/data/dn481501.aspx)

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!

like image 431
LukeHennerley Avatar asked Nov 30 '15 15:11

LukeHennerley


People also ask

How can I make my first migration?

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.

Can we run SQL script using code First migrations?

First you need to create a migration. Then in the generated migration file you can write your SQL.


1 Answers

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;
like image 199
bubi Avatar answered Sep 28 '22 13:09

bubi