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