EF migration shows empty Up() Down() methods

拥有回忆 提交于 2019-12-03 06:28:18

问题


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 another programmer so I am assuming I am doing something wrong here.

In my model there are around 30 classes, and inside the model folder there is a mapping folder and it contains the mappings for those 30 classes.

So now I added 1 new class in the same manner as those previous classes and then run the add-migration command in the Package Manager Console.

Infortunately I get an empty migration Up() and Down() method.

When I look in the database there is a __migrationHistory available with the previous 2 migrations. If I run my application now, the third migration is also added but obviously the new table is not being created because it's not in the Up() method.

What could I be doing wrong?

I think something is going wrong when scaffolding the previous migrations... It's like it can't find the new Code-First classes I have added.

This is my command:

add-migration "1.2" -verbose -ProjectName "MyEFproject"

I am assuming that the scaffolding doesn't know where to look for the new class... or is this by convention that all model classes are just expected to be in the project?

Result of add-migration:

namespace MyProject.Migrations
{
using System;
using System.Data.Entity.Migrations;

public partial class _1002 : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}
}

Sample of new Model Class:

using System;
using System.Collections.Generic;

namespace MyProject.Models
{
public partial class MyTable
{

    public string SomeId { get; set; }
    public string SomeText { get; set; }


}
}

Sample of new Mapping class

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace MyProject.Models.Mapping
{
 public class MyTableMap : EntityTypeConfiguration<MyTable>
{

    public MyTableMap()
    {
        // Primary Key
        this.HasKey(t => t.SomeId);

        // Properties
        this.Property(t => t.SomeText)
            .IsRequired()
            .HasMaxLength(30);



        // Table & Column Mappings
        this.ToTable("MyTable", "database");
        this.Property(t => t.SomeId).HasColumnName("SomeId");
        this.Property(t => t.SomeText).HasColumnName("SomeText");


    }




   }
}

Thank you,


回答1:


You need to add your table to your implementation of the DbContext class, e.g.

public class MyDatabaseEntities : DbContext {
    public virtual DbSet<MyTable> MyTable { get; set; }
}



回答2:


I was able to fix this issue by deleting a record of last migration from _MigrationHistory table. This record had been incorrectly created before I added DbSet for new model object to DbContext class. After this deletion new migration was created with correct Up() and Down() methods.




回答3:


While rolling back an existing EF Core Data Context back to empty, my migrations wouldn't generate until I removed the ApplicationDbContextModelSnapshot that accompanied the migrations.

This class is auto-generated and needs to align with your current migration level.




回答4:


I had this problem because I forgot to add {get; set;} after my variable names




回答5:


In my case, the datacontext project is a class lib project. It is different from the startup project which is asp.net mvc 5 project. Now by mistake the connection string in the startup project is pointing to a different different database.

So ensure that datacontext project and startup project point to the same database. Also use the full command as mentioned in the question like the following. You can include -Force as well.

add-migration "InitialMigration" -verbose -ProjectName "MyEFproject" -Force



回答6:


Also: Make sure any new properties you've added are public!

In my case I was doing a migration where I added fields to an existing table and was ending up with empty Up and Down methods,

I had something like this:

public bool ExistingField { get; set; }
bool NewField { get;set; }

Can you spot the difference...?


If you make this mistake rerun the migration with the same name (you probably will need to add the -Force parameter to scaffold it full).

PS. Always make sure your project builds fully before attempting to do any kind of EF command. If your project doesn't already build you're asking for trouble.




回答7:


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.




回答8:


I had to Update-Database with the latest migration before the empty one appending this parameter -TargetMigration:"{your-migration-name}".

Probably it will tell you that there will be data loss from the next buggy one we tried. If you can afford it append -Force to it.

Then I tried to add my new Add-Migration and it wasn't empty.

Final thing that you may need to do if above is throwing exception is to go SQL Server Management Studio and delete the last Automatic migration and try to add it again.




回答9:


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.




回答10:


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.




回答11:


To me the problem was that Id property that should correspond to table id was named FeedbackId. I changed to "Id" and then Up/Down weren't empty anymore. Dunno if that can help somehow



来源:https://stackoverflow.com/questions/22708519/ef-migration-shows-empty-up-down-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!