Proper implementation of an Identifying relationship in Entity Framework?

时间秒杀一切 提交于 2019-12-25 06:47:28

问题


Let's say I have an entity Author which has a name can write 0 .. 1 .. or many of the entity Book. The Book must be written by one Author, and it is not meaningful to talk of a Book with no Author.

This (I believe) would be classified as an identifying relationship. With the EntityTypeConfiguration, would the following be the correct way of implementing this identifying relationship?

public BookMapping()
{
    HasRequired(book => book.Author)
   .WithMany(author => author.Books)
   .HasForeignKey(book => book.AuthorID);
}

回答1:


It is not identifying relation. It is just common one-to-many relation. To make it identifying you must also map composite primary key consisting of BookID and AuthorID.

HasKey(book => new { book.BookID, book.AuthorID });


来源:https://stackoverflow.com/questions/9579602/proper-implementation-of-an-identifying-relationship-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!