Question

I have all my ef code first migrations in a separate assembly called Migrations

From the package manager console I type Add-Migration xyz which scaffolds up a migration

Is it possible to do this from outside of visual studio? I'm using rake scripts to do lots of automation for my build but this is one part that I've not quite got yet. The goal here would be to do the following

rake db:add_migration "xyz"

This would run some command and add the migration to the specified project. That's the only bit I've not been able to figure out how to automate yet! I'd probably build in other tasks along with this like dropping and creating the database and the getting the migration out to a script so it could be run under my roundhouse migrations.

Related material

Add-Migration [-Name] <String> [-Force]
[-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
[-IgnoreChanges] [<CommonParameters>]

Command Reference

I can see that EF is installed to the packages folder from nuget

packages\EntityFramework.5.0.0\tools

And I can see in the file EntityFramework.psm1

function Add-Migration
{
    [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName')]
    param (
        [parameter(Position = 0,
....
}

But I'm not sure how to execute it from the command line. I've tried

..\packages\EntityFramework.5.0.0\tools>powershell EntityFramework.psm1 Add-Migration

but this results in an error

The term 'EntityFramework.psm1' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, 
or if a path was included, verify that the path is correct and try again.
At line:1 char:21
    + EntityFramework.psm1 <<<<  Add-Migration
    + CategoryInfo          : ObjectNotFound: (EntityFramework.psm1:String) [],
                              CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Was it helpful?

Solution

Looking in EntityFramework.psm1 it is merely a scripting facade for the EntityFramework.PowerShell.dll. The actual add-migrations functionality is implemented in the System.Data.Entity.Migrations.AddMigrationCommand class in that assembly.

Looking at the source, it gets the currently active project (I assume that's the project that's selected in the powershell console) through this line:

get { return (Project)_domain.GetData("project"); }

Project is EnvDTE.Project which (if I google correctly) is a way to interface with the IDE. Some further source reading reveals that files are added to the project by interfacing with the IDE.

To me it looks like the scaffolding code is too heavily integrated with the Visual Studio to be possible to run outside of Visual Studio as part of a command line build.

Edit

I'm getting back to this and figured out that it might be possible. There's another SO question that describes how to use EnvDTE outside of visual studio, e.g. from command line apps:

Open DTE solution from another program (not add-in)

So it might indeed be possible to write an own .exe wrapper around the EntityFramework.PowerShell.dll if the app domain is prepared with the proper EnvDTE objects before calling the AddMigrationCommand class. To do that you would have to analyze the Entity Framework source code to know how to fool the scaffolding code to beleive it runs inside of Visual Studio.

So, in the end: It might be possible - but it will be a non trivial project to write your own tool for it.

OTHER TIPS

The EF Powershell commands must be run from within Visual Studio in order to work. From command line, you can use migrate.exe, but it does not support adding new migrations.

I had a similar problem (automate add-migration) and came up with the solution shown here.

You can create a console app that opens up a headless visual studio instance to get a project reference. With the project reference, you can call directly into the EntityFramework code (not through the Powershell) to trigger a migration and get the code back.

Once you have the code, you can send it where ever you want (like into a file that is added to the project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top