EF Add-Migration script to auto build task

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

In the package manager console, enter Add-Migration xyz , which evens the migration

Can this be done outside the visual studio? I use rake scripts to do a lot of automation for my build, but this is one of those parts that I haven't received yet. The goal here would be to:

rake db:add_migration "xyz"

This will lead to the execution of some command and the addition of migration to the specified project. This is the only bit that I have not yet been able to figure out how to automate! I would probably create other tasks along with this, like deleting and creating a database and getting migration to a script, so it could be run under my round migrations.

Related material

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

Command Reference

I see that EF is installed in the packages folder from nuget

 packages\EntityFramework.5.0.0\tools 

And I can see in the EntityFramework.psm1 file

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

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

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

but this leads to 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 
+4
source share
3 answers

Looking at EntityFramework.psm1 , it's just a script facade for EntityFramework.PowerShell.dll. The actual add-migration functionality is implemented in the System.Data.Entity.Migrations.AddMigrationCommand class in this assembly.

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

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

Project - EnvDTE.Project , which (if I use Google correctly) is a way to interact with the IDE. Some additional reading of the source indicates that the files are being added to the project, interacting with the IDE.

It seems to me that the scaffolding code is too tightly integrated with Visual Studio to run outside of Visual Studio as part of the command line assembly.

Edit

I come back to this and realized that this is possible. There's another SO question that describes how to use EnvDTE outside of a visual studio, for example. from command line applications:

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

Thus, it would really be possible to create your own .exe wrapper around EntityFramework.PowerShell.dll if the application domain is prepared with the corresponding EnvDTE objects before calling the AddMigrationCommand class. To do this, you will need to analyze the source code of the Entity Framework to find out how to trick the forest code into working inside Visual Studio.

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

+5
source

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

+4
source

I had a similar problem (add-migration automation) and got the solution shown here .

You can create a console application that will open a copy of the visual studio without a head to get a link to the project. With a link to the project, you can directly call the EntityFramework code (and not through Powershell) to trigger the migration and return the code.

Once you have the code, you can send it wherever you want (for example, to a file added to the project.

0
source

Source: https://habr.com/ru/post/1486074/


All Articles