问题
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