Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run an Entity Framework 7 migration on a remote database using Powershell?

In a standalone entity framework 7 project (note, not a MVC project with an entity context where the command DNX might be used), how can I run a migration on a remote database using Powershell?

I am currently using Entity Framework 7.0.0-rc1-final with Visual Studio 2015 (14.0.23107.0).

like image 317
StampyTurtle Avatar asked Dec 02 '15 16:12

StampyTurtle


People also ask

How do I run an existing migration in Entity Framework?

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.

Which command of Entity Framework core will migrate the model to database?

Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.

How do I add a migration to multiple DbContext?

One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.

Which command generates a SQL script from migration?

With From and To The following generates a SQL script from the specified from migration to the specified to migration. You can use a from that is newer than the to in order to generate a rollback script.


2 Answers

At the moment there is no way you can use pure PowerShell to do this because a utility like migrate.exe does not exist yet and importing the EF PS modules is not possible as they require a Package Manager PowerShell Host.

Here are some ideas how you can update a remote db in EF7:

One thing you could do is use the package manager console commands from within VS as usual to update the remote db. You can create a second context that has the remote db connection string and use the update-database command specifying the context to use. These commands require the following package in EF7: https://www.nuget.org/packages/EntityFramework.Commands/. I have done this successfully in a class lib project.

Another solution would be to use DNX commands by creating a DNX project instead of a classic one. DNX projects are not just for web sites, it is just another type of project. Here is a link that shows how to create a console app DNX project: http://docs.asp.net/en/latest/dnx/console.html. So with this type of project you can use the provided DNX commands that you seem to be aware of.

I hope this helped. Maybe we can give more help if you describe your situation and your end goal in more detail.

like image 177
Ares Avatar answered Oct 14 '22 23:10

Ares


Answer too long as a comment, so adding it here...

Have you looked at this article and the links in the answer?

From that answer

The problem with importing the module into a PowerShell console is that I believe the module expects to run in a context where it has a Visual Studio DTE object available. That environment is the NuGet Package Manager Console. This issue has been brought up before. Check out this blog post and this SO question.

This blog post shows how to write code that does migrations.

What might be helpful for readers of this question is what you have tried, what is not working, and other information that might help solve your problem.

like image 45
Kory Gill Avatar answered Oct 14 '22 23:10

Kory Gill