EF Core空迁移问题

时间:2018-09-06 06:57:20

标签: ef-migrations ef-core-2.1

我正在使用.Net Core 2.1和EF Core 2.1。我使用EF Code First Migrations成功创建了2个表,并且dotnet CLI还向其中植入了数据。但是,当我尝试添加新的模型功能并运行迁移时,生成的文件具有空的Up()和Down()方法,并且数据库的EF_MigrationsHistory表中没有条目,并且modelSnapshot.cs文件也没有包含对特征模型的任何引用。我交叉检查了ApplicationDbContext,以查看是否意外错过了类中的模型声明,但我没有。我不确定这个问题。谁能帮我这个忙吗?从我的项目中发布代码:

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(迁移文件):

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数据库映像:

enter image description here

1 个答案:

答案 0 :(得分:0)

通常,在种子数据文件中包含context.Database.EnsureCreated();时会发生这种情况。此方法不允许创建迁移表,因此不能与迁移一起使用。您需要使用Remove-Migration命令删除迁移,或者删除服务器中的Migration文件夹和数据库并创建新迁移。检查Microsoft docs以获得更好的理解。