none
Why doesn't Add-Migration command auto generate the migration from my newly created model? RRS feed

  • Question

  • I ran the following command, Add-Migration InitialCreate, after creating my first model and it auto generated the Up and Down methods from that file. However, every time I create a model now and then run the Add-Migration command it just gives me a skeleton migration file. What am I doing wrong?
    Sunday, July 27, 2014 12:52 AM

Answers

  • Hello,

    Firstly, I'm glad to hear that you got it working.

    >> Not sure if the way I'm handling this is the correct way or not.

    Yes, you are right, it is not recommended to use multiple contexts in one project and we usually put entities under one context, this would let us manage the context and its entities easier.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by moopasta Wednesday, July 30, 2014 12:12 PM
    Wednesday, July 30, 2014 5:45 AM
    Moderator

All replies

  • I don't know anything about Entity Framework which seems to be what Add-Migration is used for or with I guess.

    Perhaps you need to ask your question in another forum like at ASP.Net - Entity Framework or ADO.NET Entity Framework and LINQ to Entities perhaps.

    Also see;

    Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application

    Entity Framework (EF) Documentation

    Or maybe someone on this Visual Basic .Net forum can assist you and maybe sooner or later you'll get a reply.


    La vida loca

    Sunday, July 27, 2014 3:08 AM
  • Mr. Monkeyboy, ahh thanks. New to these parts and didn't realize there was a more relevant forum. Thanks!
    Sunday, July 27, 2014 3:53 PM
  • Hello,

    >>However, every time I create a model now and then run the Add-Migration command it just gives me a skeleton migration file    

    Could you please clarify how you create a model and then run the Add-Migration command? What about your original model, you delete it or not?

    The process to use migration is: We firstly create the model, run enable-migrations to enable migration, add an initialize migration and run update-database to generating database, then we do some changes to our current model, we add migration for these changes and run update-database to apply changes to database. So I am a bit confused why you create a model, is it a new model? If it is, this is not necessary.

    For detail regarding code first migration, you can refer to this link:

    http://msdn.microsoft.com/en-us/data/jj591621.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 28, 2014 8:23 AM
    Moderator
  • Fred Bao, hey thanks for your reply.  So the process I'm doing at the moment is the following:

    1. Create Visual Studio Project (MVC)
    2. Add class file to the Models directory (ex: User.vb)
    3. Then run in Package Manager Console: `enable-migrations` & afterwards `add-migration intialcreate`

    This works; however, if I try to then create another model in the Models directory like 'School.vb' and then run `add-migration AddedSchoolModel` it doesn't auto generate.  It just shows and empty up/down file.

    Monday, July 28, 2014 8:33 PM
  • Hello,

    >> if I try to then create another model in the Models directory like 'School.vb' and then run `add-migration AddedSchoolModel` it doesn't auto generate.  It just shows and empty up/down file.

    This is because your migration is enabled for User.vb, after you add migration addedschoolmodel, it is equivalent to add an empty for User.vb because the migration is enabled only for User.vb at that time.

    For multiple context, you need to do some additional work:

    Creating the initial migration:

    1. In Package Manager Console:

       Enable-Migrations -ContextTypeName User

    2. In Solution Explorer: Rename Configuration.cs to ConfigurationUser.cs

       Edit ConfigurationUser.cs: Change the namespace to Migrations.User

    3. Enable-Migrations -ContextTypeName School

    4. In Solution Explorer: Rename Configuration.cs to ConfigurationSchool.cs

       Edit ConfigurationSchool.cs: Change the namespace Migrations.School

    5. add-migration InitialSchoolSchema -ConfigurationTypeName ConfigurationSchool

    6. Update-Database -ConfigurationTypeName ConfigurationSchool

    7. add-migration InitialUserSchema -ConfigurationTypeName ConfigurationUser

    8. Update-Database -ConfigurationTypeName ConfigurationUser

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 29, 2014 8:08 AM
    Moderator
  • Thanks Fred Bao! So I've changed the way I'm setting it up slightly after reading up some and your answers.  I'm unsure if I need a new context for each Model, which is what I thought at first.  At the moment I have it setup as follows:

    ModelBase.vb

    Public Property ID as Integer
    Public Property CreatedAt as Integer
    Public Property UpdatedAt as Integer

    Public Class ModelContext
        Inherits DbContext
        Public Property Users() As DbSet(Of User)
        Public Property Schools() As DbSet(Of School)
        Public Property Players() As DbSet(Of Player)
    End Class

    Then my Models just have their own properties.  When I did the migration this time it seemed to work fine.  I imagine the only problem with this setup is that if I create a Model without inheriting ModelBase I would have to then create another context instead of just adding a new property to the ModelContext.  Not sure if the way I'm handling this is the correct way or not.  It does seem to work correctly though now.

    Tuesday, July 29, 2014 2:44 PM
  • Hello,

    Firstly, I'm glad to hear that you got it working.

    >> Not sure if the way I'm handling this is the correct way or not.

    Yes, you are right, it is not recommended to use multiple contexts in one project and we usually put entities under one context, this would let us manage the context and its entities easier.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by moopasta Wednesday, July 30, 2014 12:12 PM
    Wednesday, July 30, 2014 5:45 AM
    Moderator