EF Code First Migrations, Update-Database outside of Visual Studio

In a recent blog post comment, someone asked “can you please tell them [EF Dev Team] some developers would like to use the Power Shell command script and not PM console to update database.”

If you look inside the package folder for Entity Framework 4.3 (or 4.3.1 or whatever is the current version in the future) there’s a tools directory and inside of there a migrate.exe command. It is the same as the update-database command with all of the same parameters.

migratecommandline

Shout it
#1 Ed N on 3.12.2012 at 11:19 AM

That as me and you are a sweetie!

But still would like to see more of those famous Julie Lerman videos on EF but with PS and not PM.

:)

#2 Julie on 3.12.2012 at 11:21 AM

Ed...assuming PS is Pluralsight. What's PM?

#3 Ed N on 3.12.2012 at 11:25 AM

Power Manager Console.

Do not get me wrong, I see it's value, but I wish someone would do a simple video on incorporating all the goodies of Nuget's PM in PS. I tried to command line import Nuget's scaffold cmdlets, as example, it won't work, at least for me.

And that is a shame, as I can see the advantage of using Nuget's scaffold directly in Powe rShell, to change a client's MVC website, without needing to go into VS.

In today's economy, the name of the game is instant (automatic) modification.

#4 Ed on 3.12.2012 at 11:28 AM

Sorry Julie, guess I am going abbreviation crazy, when I used PS above, I meant PowerShell.

#5 Julie on 3.12.2012 at 11:29 AM

oh... :) ps==powershell. silly me. I'll see if anyone's working on that for pluralsight.

#6 James on 3.12.2012 at 2:47 PM

Love your books/video snippets. Keep up the good work. You're indispensable. You have made Entity Framework shine!

#7 Fran M on 3.15.2012 at 2:16 PM

Julie is there a way to update database in the code behind and not use Package Manager Console?

If so can you kindly provide an example?

Thank You.

#8 Julie on 3.15.2012 at 2:24 PM

@Fran M. Yes it is possible. If you have a pluralsight.com subscription, I did cover it in my recent course on Migrations. Otherwise, check out this post from Rowan Miller: romiller.com/.../running-scripti

#9 Fran M on 3.15.2012 at 2:45 PM

Do you know which video title in Plural Sight migrations this topic is in?

#10 Julie on 3.15.2012 at 2:54 PM

@Fran: It's actually titled Entity Framework Code First Migrations! :)

Here's a link: www.pluralsight-training.net/.../TableOfContents

#11 Fran M on 3.15.2012 at 6:15 PM

Julie, I saw the video that will make changes thru the code behind. However, unlike you, I must do a clean and then rebuild of my Console App. If I do a straight build, the columns are not yet reflective of the changes in the model.

What could I be doing wrong?

PS: Decided to make Configuration public and call

Database.SetInitializer(new MigrateDatabaseToLatestVersion<ProductContext, Configuration>());

#12 Julie on 3.15.2012 at 7:45 PM

Hi Fran,

How do you intend to perform migrations from code? I would use it in a very explicit utility. Not run it every time the app is started. If you prefer, just send me an email through my contact form.

#13 Fran M on 3.20.2012 at 5:42 PM

Julie:

I emulated your plural sight migrations demo & database updates in code.

Question:

(I) I have a entity called "FavThings" and it has a sole prop called Id in the Domain project.

(II)In MVC/WPF project

3 times user submits form w/2 txtbxs:

Enters "Holiday" & "string" & submits

Then enters "Food"& "string" & submits

Then enters "Age" & "int" & submits

With ea. form submit,

1. New prop name (txtbx1) & type(txtbx2) written in FavThings.cs file as primitives (from Domain project #I above) and then updates database.

All done, user clicks link to diff page, titled "My Fav Things", which has a form with 3 textboxes labeled "Holiday", "Food" and "Age". User submits and row added to table.

Is this possible? cannot find examples. Help?

#14 Fran M on 3.20.2012 at 5:47 PM

One last thing: Be advised that my existence depends on a solution. :)

#15 Julie on 3.20.2012 at 6:24 PM

The code lets you execute a migration, not create one. So from everything I know, this can't be done *this* way. It would require two impossible things: 1) your class would need to get updated to reflect the new properties and 2) code first needs to create a migration. Peopl have asked about letting endusers affect the model, the classes and teh database for years. The answer has always been no. There might be some way to pull it off but it would require reflection and all kinds of very internal stuff. Feel free to run it by someone on the EF team but as far as I know there has never been a way to do this with EDMX. I don't know how you could possibly do it with code first. (Honestly I just want to say "NO" period, but your last comment makes that difficult. ;) )

#16 Julie on 3.20.2012 at 7:20 PM

Fran, thinking more...automatic migrations get you closer. But you still have to have a way of redefining the class so that migrations will see the change.

#17 Fran M on 3.20.2012 at 7:37 PM

If there was a way I could get the path of the entity class file in another project, I can write to it.

I am trying to work with what little examples there are on migrate.exe and this is the command line syntax:

migrate.exe Web.Data <-- MyAssembly

/startupdirectory:Web.MVC\bin\Debug

/connectionProviderName:"System.Data.SqlClient"

/connectionString:"Data Source=.\SqlExpress;Initial Catalog=Snapple;Integrated Security=True;MultipleActiveResultSets=True"

/verbose

But I get an error that Assembly can not be found/loaded.

#18 Fran M on 3.20.2012 at 7:43 PM

Am I totally off my rocker thinking I can write to an entity cs file and then command line migrate the database while on the very compiled-version of app?

#19 Peter on 3.22.2012 at 10:40 PM

Fran, yes, that is crazy talk. I have no dog in this fight, in fact I'm only here because I saw the tweet on data migrations and was scrolling through the comments when I saw your 5 comments.

I just want to say, whatever you're planning on doing, you should probably not do.

With love,

Peter

#20 Sonali Noolkar on 3.30.2012 at 9:52 AM

Hi Julie,

Not sure whether I can post my questions here for EF 4.3

I need to implement CRUD operations using stored procedures.

Does EF 4.3.1 supports Stored Procedures?

Can you provide some guidance on this?

Leave a Comment