当我想在实体框架上创建一个具有流畅api的多级表时,我得到一个错误。
but I got fail:
SQLite Error 19: 'FOREIGN KEY constraint failed'.
表结构如下:
Base Entity just for Id
public class MainArt : BaseEntity
{
public Paper Paper { get; set; }
public int PaperId { get; set; }
}
public class Paper : BaseEntity
{
public string Name { get; set; }
public string PaperPrice { get; set; }
public ColorType ColorType { get; set; }
public int ColorTypeId { get; set; }
}
public class ColorType : BaseEntity
{
public string Name { get; set; }
}
MainArt——比;纸——比;ColorType
EF Fluent如下:
MainArt:
public void Configure(EntityTypeBuilder<MainArt> builder)
{
builder.Property(p => p.Id).IsRequired();
builder.HasOne(p => p.Paper).WithMany().HasForeignKey(p => p.PaperId);
}
:
public void Configure(EntityTypeBuilder<Paper> builder)
{
builder.Property(p => p.Id).IsRequired();
builder.Property(p => p.Name).IsRequired().HasMaxLength(100);
builder.Property(p => p.PaperPrice).IsRequired();
builder.HasOne(p => p.ColorType).WithMany().HasForeignKey(p => p.ColorTypeId);
}
}
ColorType和Paper使用迁移创建,但MainArt失败。
我搜索了网页,我想我错过了一些东西。ToTable还是一对多我不知道
谢谢。
我发现为什么它给出例外。代码没有错。
创建子表失败,导致错误。
当我正确创建子表时,迁移和播种工作正常。