EF migration shows empty Up() Down() methods

后端 未结 17 2208
花落未央
花落未央 2020-12-25 09:56

I have a local database that is currently in it\'s second version and should now go to it\'s third version.

The code for the previous migrations was generated by ano

相关标签:
17条回答
  • 2020-12-25 09:58

    Temprorary remove

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
    

    and then do initial create

    Add-Migration InitialCreate
    
    0 讨论(0)
  • 2020-12-25 10:02

    From the perspective of a complete Entity Framework (Core) beginner:

    1. Create your class which will become your table
      1. You can have subclasses with many-to-many or one-to-one relationships.
      2. In step 3 you see the context where both properties have a one-to-one relationship.
    2. Ensure you have one DbContext
      1. If you have more than one DbContext you need to specify which context you want to add the migration to with the -Context parameter.
    3. Add your class to your DbContext as shown by @CondingIntrigue
      1. As a reference The Entity Framework Core DbSet
    public class AccountContext : DbContext
    {
            public DbSet<Account> Accounts { get; set; }
            public DbSet<SecretIdentity> SecretIdentity { get; set; }
    }
    
    1. Enter Add-Migration
    0 讨论(0)
  • 2020-12-25 10:03

    I missed adding

    {get;set}
    

    After adding getter and setter, up and down methods are not empty.

    0 讨论(0)
  • 2020-12-25 10:05

    I had this exact issue after I wanted to add an extra column to my database. Because my data would not seed unless the tables were empty, I deleted all the tables and the migrations to recreate the tables. When I tried to migrate, the migration had empty up and down methods.

    I solved this by deleting the snapshot file as this was creating the issue. So I deleted all the migrations and the snapshot file, added the migration again and ran update database. The tables and migrations were successfully updated with my new column.

    A better way to do this though is to run the down method and drop the tables like that if you are working on test data. Obviously this is bad in the real world to drop tables.

    0 讨论(0)
  • 2020-12-25 10:07

    If your project is small, i.e. you do not have too many migrations yet, you can delete all from your Migration folder. After that, add the migrations again.

    0 讨论(0)
  • 2020-12-25 10:08

    I was getting empty migrations added when I had mistakenly related two tables using a 1-many relationship rather than a many-many (i.e. i forgot one of the navigation properties). I had a seeding file that was expecting a many-many relationship and was subsequently failing during the migration causing the migration to fail. Unfortunately there was no output that made it obvious that was the problem and it was only by using the Entity Framework Power Tools (v4 but installed in VS2015) did i visually see the incorrect relationship and realize it was probably the cause.

    0 讨论(0)
提交回复
热议问题