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

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

22 thoughts on “EF Code First Migrations, Update-Database outside of Visual Studio

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

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

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

  5. 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>());

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

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

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

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

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

  11. 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?

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

  13. 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?

  14. I have an issue similar to the rest. I use VS internet services template and build my application and publish and it works. Then over a short period of time, I get this error while trying to log on.
    Error.
    An error occurred while processing your request.
    Several time I was able to replace the publish profile with success, but most of the time I need to rebuild the application using a fresh template.
    I don’t think I have an update database from console problem/solution here, I think it is much simpler. I am cleaning and rebuilding with no success. hmmm?

  15. John, If it doesn’t have anything to do with EF migrations (the focus of this 3 year old post), you might be better off on stackoverflow targeting the relevant technology. I really wouldn’t know where to begin. Sorry.

Leave a Reply to James Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.