2
我正在將具有EF Core項目的ASP.NET Core 1.1升級到ASP.NET Core 2.0。實體類型需要在EF Core 2.0中定義主鍵
我有以下實體和配置,這是之前的工作:
public class Ebook {
public Int32 Id { get; set; }
public String Title { get; set; }
public virtual ICollection<EbookFile> EbookFiles { get; set; } = new List<EbookFile>();
}
public class EbookFile {
public Int32 EbookId { get; set; }
public Int32 FileId { get; set; }
public virtual Ebook Ebook { get; set; }
public virtual File File { get; set; }
}
public class File {
public Int32 Id { get; set; }
public Byte[] Content { get; set; }
public virtual ICollection<EbookFile> EbookFiles { get; set; } = new List<EbookFile>();
}
而且配置爲:
builder.Entity<Ebook>(b => {
b.ToTable("Ebooks");
b.HasKey(x => x.Id);
b.Property(x => x.Id).ValueGeneratedOnAddOrUpdate().UseSqlServerIdentityColumn();
b.Property(x => x.Title).IsRequired(true).HasMaxLength(200);
b.HasIndex(x => x.Title).IsUnique();
});
builder.Entity<EbookFile>(b => {
b.ToTable("EbookFiles");
b.HasKey(x => new { x.EbookId, x.FileId });
b.HasOne(x => x.Ebook).WithMany(x => x.EbookFiles).HasForeignKey(x => x.EbookId).IsRequired(true).OnDelete(DeleteBehavior.Cascade);
b.HasOne(x => x.File).WithMany(x => x.EbookFiles).HasForeignKey(x => x.FileId).IsRequired(true).OnDelete(DeleteBehavior.Cascade);
});
builder.Entity<File>(b => {
b.ToTable("Files");
b.HasKey(x => x.Id);
b.Property(x => x.Id).UseSqlServerIdentityColumn();
b.Property(x => x.Content).IsRequired(false);
});
而且在我的語境I類具有的屬性:
public DbSet<Ebook> Ebooks { get; set; }
public DbSet<EbookFile> EbookFiles { get; set; }
public DbSet<File> Files { get; set; }
當我運行它時,我收到錯誤:
The entity type 'EbookFile' requires a primary key to be defined.
我在這裏錯過了什麼? EF Core 2.0有所改變嗎?
複合主鍵仍然工作在我的項目很好,所以單獨不能是吧。 –
刪除'.ValueGeneratedOnAddOrUpdate()'。嘗試生成遷移給出*「無法將屬性'Id'配置爲'ValueGeneratedOnUpdate'或'ValueGeneratedOnAddOrUpdate',因爲在將實體添加到商店後無法更改密鑰值。」* –
@Stoev我將其刪除不斷得到相同的錯誤... –