2017-01-19 1 views
0

데이터베이스의 특정 항목을 참조하고 싶습니다. 사용할 수있는 데이터 특수 효과가 있습니까? 예.Entity Framework 코드 우선 - 외래 키/탐색 속성 설정

public class Address 
{ 
public int CityId {get; set;} 
} 

public class City 
{ 
public int id {get; set;} 
} 

그래서 Address.CityId는 City.id를 참조합니다.

데이터 주석을 통해 표 열을 어떻게 참조 할 수 있습니까?

답변

0

. 다음과 같이 당신은 당신의 Address 클래스에서 탐색 속성을 만들 수 있습니다

public class Address 
{ 
[Key] 
public int AddressID {get;set;} 
public int CityID {get; set;} 
[ForeignKey("CityID")] 
public City City {get;set;} 
} 

public class City 
{ 
[Key] 
public int CityID {get; set;} 
} 

추가 ForeignKey 속성을 탐색 속성에 외래 키를 매핑을 만들 수 있습니다. 그리고 각 EF 모델에서 핵심 속성을 정의하는 것을 잊지 마십시오.

Entity Framework 홈 페이지에 대한 자세한 내용 : Configure One-to-Zero-or-One Relationship

관련 문제