My Code-First 디자인에서 Independent Association과 Foreign Key Association을 모두 정의해야한다는 결론에 도달했습니다. 예컨대 : 위의 정의와Entity Framework 4.1의 외래 키 연결 업데이트 코드 우선
public class Book
{
public int ID {get; set;}
public int AuthorID {get; set;}
[ForeignKey("AuthorID")]
public Author Author {get; set;}
}
, 나는이 책의 저자를 변경하려면, 또는 바로 아래 라인이 충분히 사용할 때이 AuthorID를 업데이트해야합니까?
myBook.Author = author;내가이 책의 저자를 정의하는 것이 처음이라면 위의 줄에 null 예외가 발생합니까? (내가 몇 가지 값을 할당 할 때합니까 EF는 자동으로이 책의 저자를 초기화?) 나는 정의에 초기화해야 :
는
코드 : 모든
public class Book
{
public int ID {get; set;}
public int AuthorID {get; set;}
private Author m_Author;
[ForeignKey("AuthorID")]
public Author Author {get
{
get
{
if (m_Author == null)
m_Author = new Author();
return m_Author;
}
set
{
this.m_Author = value;
}
}
}
가에서 "만들기 및 수정 관계"에서 두 번째 예를 살펴 보시기 바랍니다 : HTTP : // MSDN을 .microsoft.com/kr/library/ee373856.aspx 탐색 속성을 할당해도 예외가 throw되지 않는 이유는 무엇입니까? (두 번째 예제에서는 navigatrion 속성 만 업데이트됩니다.) – Kamyar