エンティティ フレームワーク 5.0
次の設定があるとします。
public class Book
{
public int ID {get; set;}
public string Title {get; set;}
[InverseProperty("Books")]
[Required]
public Author Author {get; set;}
}
public class Author
{
public int ID {get; set;}
public string Name {get; set;}
public virtual ICollection<Book> Books {get; set;}
}
次に、コードで新しい Book を作成し、次のようにします。
author.Books.Add(newBook);
毎回これを書くのではなく、ブックにその著者を自動的にピックアップさせるにはどうすればよいですか:
newBook.Author = author;
親のコレクションに追加されたときに、子エンティティがその親を自動的に取得するようにします。
これは可能ですか?流暢なマッピングかな?
それとも、この双方向の関係の両側を自分で維持する必要がありますか?