locked
MVC migrations - Empty Up/ Down methods RRS feed

  • Question

  • User-280480587 posted

    Hi

    I am trying to use migrations but they seem to be empty after creating for a new model.

    I have created a test 'asp web application' using MVC and included authentication for 'individual user accounts'

    I then enter the following in the console:

    Enable-Migrations

    Create-Migration Initial

    Update-Database

    I then create a new model with some test properties, and the create a new migration as above with out the 'Enable-Migrations' part.

    I get no errors but my new migration is empty. Why is this. What have i missed?

    Thanks

    Tuesday, November 20, 2018 8:42 PM

All replies

  • User-271186128 posted

    Hi Izeko2004,

    I then create a new model with some test properties, and the create a new migration as above with out the 'Enable-Migrations' part.

    I get no errors but my new migration is empty. Why is this. What have i missed?

    If you want to add a new model, after adding the model class file, you need to add the DbSet properties in your DBContext class. 

    like this sample: after adding the Student class, add the DbSet property in the SchoolContext

        public class SchoolContext: DbContext 
        {
            public SchoolContext(): base()
            {
                
            }
                
            public DbSet<Student> Students { get; set; }
            public DbSet<Grade> Grades { get; set; }
        }

    Then, using Add-Migration command to generate a migration class, finally, update the database using the Update-Database command.

    More details about EF Code First, please refer to the following links:

    http://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx

    http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx

    Best regards,
    Dillion

    Wednesday, November 21, 2018 3:32 AM