7

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

特征.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 数据库图像:

在此处输入图像描述

4

2 回答 2

4

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

于 2018-09-06T07:11:48.067 回答
0

我遇到了类似的问题。Up尽管清理了迁移文件夹并运行dotnet ef migrations add Initial了多次,但该方法为空。

我的解决方案是更改 DbSet 属性并添加virtual到每个属性中。

代替

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

尝试

public virtual DbSet<Make> Makes { get; set; }
于 2022-01-06T02:23:25.580 回答