15

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).

StampyTurtle
  • 2,728
  • 2
  • 14
  • 16

2 Answers2

6

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.

Ares
  • 1,356
  • 1
  • 9
  • 22
  • **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.** This is good to know; I didn't realize that the EF PS commands weren't usable outside of the VS Package Manager PS Host. – StampyTurtle Jan 12 '16 at 14:09
  • **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 commend 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.** I'll try going down this route as I think this would accomplish my goal of being able to target individual remote databases with different credential schemes using powershell – StampyTurtle Jan 12 '16 at 14:12
  • @StampyTurtle I am happy this idea is of help. – Ares Jan 13 '16 at 09:34
  • Thanks for the bounty :) – Ares Jan 13 '16 at 09:35
1

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.

Community
  • 1
  • 1
Kory Gill
  • 6,993
  • 1
  • 25
  • 33