How to run Code-First Migrations from a psake build?
Asked Answered
U

2

5

I can type Update-Database, Enable-Migrations etc, from Package Manager Console and it works fine.

If I need to do the same from a regular powershell session, or in a psake build file, then how do I do it?

I tried importing the module EntityFramework.5.0.0\tools\EntityFramework.psm1 from the packages directory, and I did get the Update-Database, Enable-Migrations functions, but I cannot supply their arguments - they need a project, source, and 6 more - and there is no documentation whatsoever. Can I not automate the database deploy on some machine in our CI chain ?

Uncial answered 2/9, 2012 at 18:32 Comment(0)
P
6

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.

Piezoelectricity answered 4/9, 2012 at 3:19 Comment(4)
Hey thanks for answering, So is the only way to run migrations from build scripts is by generating SQL, and using that?Uncial
@Uncial I don't know if that is the only way but it certainly is one way that would work. Check out these two blog posts: thedatafarm.com/blog/data-access/… and romiller.com/2012/02/09/running-scripting-migrations-from-codePiezoelectricity
Awesome~! the second link post deserves more recognition - I was about to write my own tool which will do the migrations after looking at the 1st link, that I can call from build scripts - and the second link pointed me to the already developed tool :)Uncial
@Uncial Good suggestion. I put that link in the answer.Piezoelectricity
G
0

Use migrate.exe from EntityFramework NuGet package.

https://mcmap.net/q/641202/-running-update-database-from-team-city

Gaffe answered 29/4, 2016 at 5:39 Comment(1)
you can update-database from migrate.exe on the command line but you can't enable-migrations (or add-migration) this way.Unshapen

© 2022 - 2024 — McMap. All rights reserved.