Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core empty migration issue

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

like image 220
Mellionis Avatar asked Sep 06 '18 06:09

Mellionis


People also ask

Why add migration is not working?

Add-Migration - The Term 'Add-Migration' Is Not Recognized After creating models and context class, we nomally add migration to initialize the database. The error occurs sometimes while adding migration in asp.net core, entity framework with code first approach because of some missing library.

Why is my migration build failed?

It's because of other projects in your solutions. e.g If you have two projects in your solution as Project A and Project B. If Project A is not successful build and then Project B data migration will fail. So you should unload Project A and add the migration to Project B again.


Video Answer


1 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.

like image 107
Llazar Avatar answered Oct 11 '22 23:10

Llazar