【问题标题】:EF Core empty migration issueEF Core 空迁移问题
【发布时间】:2018-09-06 06:57:19
【问题描述】:

我正在使用 .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 数据库图片:

【问题讨论】:

    标签: entity-framework-migrations ef-core-2.1


    【解决方案1】:

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

    【讨论】:

    • 我确定我的代码中没有这样的东西,我检查了很多次。
    • 在运行按钮中选择项目名称而不是 IIS Express 并在 cmd 中显示错误。
    • 实际上我使用的是 VS Code 编辑器而不是 VS Studio。我也没有看到任何错误。迁移运行良好,但问题是它们在我运行时一直返回空文件。
    • 从 cmd 运行应用程序。执行后一定会在 cmd 中显示某种错误,因为在创建新表或种子数据时,您可以看到 EF 的成功或失败。
    【解决方案2】:

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

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

    代替

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

    试试

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2019-10-08
      • 2021-05-15
      相关资源
      最近更新 更多