EF Core empty migration issue

Viewed 4253

I am using .Net Core 2.1 and EF Core 2.1. I successfully created 2 tables with EF Code First Migrations and dotnet CLI also seeded data into it. However when I try to add a new model Feature, and run the migration, the file generated has empty Up() and Down() methods and there is no entry in the EF_MigrationsHistory table of Database and the modelSnapshot.cs file also doesn't contain any references to the Feature model. I cross checked the ApplicationDbContext to see if I accidentally missed the declaration of the model in the class but I hadn't. I am not sure of the issue. Can anyone help me out with this? Posting the codes from my project:

Feature.cs

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ProjectName.Models
{
  public class Feature
  {
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }
  }
}

ApplicationDbContext.cs

using Microsoft.EntityFrameworkCore;
using ProjectName.Models;

namespace ProjectName.Persitance{
public class ApplicationDbContext: DbContext{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> context)
            : base(context){ }

    public DbSet<Make> Makes { get; set; }

    public DbSet<Feature> Features { get; set; }
   }
}

20180906063933_AddFeature.cs(Migration File):

using Microsoft.EntityFrameworkCore.Migrations;

namespace ProjectName.Migrations
{
    public partial class AddFeature : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {

        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}

ApplicationDbContextModelSnapshot.cs:

// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Application.Persitance;

namespace Application.Migrations
{
    [DbContext(typeof(ApplicationDbContext))]
    partial class ApplicationDbContextModelSnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
    #pragma warning disable 612, 618
            modelBuilder
            .HasAnnotation("ProductVersion", "2.1.2-rtm-30932")
            .HasAnnotation("Relational:MaxIdentifierLength", 128)
            .HasAnnotation("SqlServer:ValueGenerationStrategy", 
              SqlServerValueGenerationStrategy.IdentityColumn);

            modelBuilder.Entity("Application.Models.Make", b =>
            {
                b.Property<int>("Id")
                    .ValueGeneratedOnAdd()
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
                    SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<string>("Name")
                    .IsRequired()
                    .HasMaxLength(255);

                b.HasKey("Id");

                b.ToTable("Makes");
            });

        modelBuilder.Entity("Application.Models.Model", b =>
            {
                b.Property<int>("Id")
                    .ValueGeneratedOnAdd()
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
                      SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<int>("MakeId");

                b.Property<string>("Name")
                    .IsRequired()
                    .HasMaxLength(255);

                b.HasKey("Id");

                b.HasIndex("MakeId");

                b.ToTable("Models");
            });

            modelBuilder.Entity("Application.Models.Model", b =>
            {
                b.HasOne("Application.Models.Make", "Make")
                    .WithMany("Models")
                    .HasForeignKey("MakeId")
                    .OnDelete(DeleteBehavior.Cascade);
            });
     #pragma warning restore 612, 618
     }
   }
 }

__EFMigrationsHistory DB Image:

enter image description here

2 Answers

Usually this happens when you include context.Database.EnsureCreated(); in your seed data file. This method doesn't allow to create a migrations table and cannot be used with migrations. You need to remove migrations with Remove-Migration command or delete the Migration folder and the database in your server and create new migration. Check Microsoft docs for better understanding.

I was having a similar issue. The Up method was empty despite cleaning the migrations folder and running dotnet ef migrations add Initial multiple times.

The solution in my case was to change the DbSet properties and add virtual to each of them.

Instead of

public DbSet<Make> Makes { get; set; }

try

public virtual DbSet<Make> Makes { get; set; }
Related