0

エンティティ フレームワーク 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;

親のコレクションに追加されたときに、子エンティティがその親を自動的に取得するようにします。

これは可能ですか?流暢なマッピングかな?

それとも、この双方向の関係の両側を自分で維持する必要がありますか?

4

1 に答える 1

0

私の間違い。

これはデフォルトの動作であり、本はすぐに使用できる Author を取得します。

ケースを閉じました。

于 2012-12-07T14:27:14.120 に答える