2012-02-10 3 views
0

내 프로젝트 중 하나에 EF를 POCO와 함께 사용하고 있습니다.엔티티 프레임 워크가 탐색 속성을로드하지 않습니다.

데이터 컨텍스트와 pocos가 성공적으로 만들어 졌으므로 데이터를 가져올 수 있습니다. 한 가지 문제가 있습니다. 탐색 속성은 항상 null입니다.

나는이 두 클래스를 가지고 :

public class UserProfile { 

public string Username { get; set; } 
public int RoleId { get; set; } 
public virtual SecurityRole SecurityRole { get; set; } 

} 

public class SecurityRole { 

public int roleId { get; set; } 
public string RoleDescription { get; set; } 

} 

public class DataContext : ObjectContext { 

public ObjectSet<UserProfile> UserProfiles { get; set; } 
public ObjectSet<SecurityRole> SecurityRoles { get; set; } 

public DataContext() : base("name=datacontext_entities", "datacontext_entities") { 

UserProfiles = CreateObjectSet<UserProfile>(); 
SecurityRole = CreateObjectSet<SecurityRole>(); 

} 

} 

데이터베이스는 사용자 프로필 및 securityrole의 개체가 포함되어 있습니다. 외래 키가 설정됩니다. 간단한 linq 쿼리를 통해 이러한 요소에 액세스합니다. Db.UserProfile.FirstOrDefault(f => f.Username == username);Db은 내 datacontext입니다.

전체 사용자 프로필 개체에는 데이터가 있지만 탐색 속성 인 SecurityRole은 항상 null입니다.

내가 누락 된 항목이 있습니까?

답변

1

EF 코드가 처음 작동하는 방식은 yourur SecurityRole 클래스의 Id 속성을 SecurityRoleId 또는 다른 것으로 사용하거나 [Key] 특성을 적용해야한다고 생각합니다. 그것은 기본 키가 SecurityRole에 대한 사실 역할 ID입니다 DB에, 다음

public class SecurityRole 
{ 
    [Key] 
    public int roleId { get; set; } 
    public string RoleDescription { get; set; } 

} 

또는

public class SecurityRole 
{ 
    public int SecurityRoleId { get; set; } 
    public string RoleDescription { get; set; } 

} 
+0

험을 작동합니다. 나는 속성을 사용하지 않으려 고 노력하지만 그 것을 확인해 보겠습니다. – Erick

+0

네, Julie Lerman의 비디오를 조금만 들여다 보면 그 부분이 정리 될 것이라고 생각합니다. EF 웹 사이트뿐만 아니라 복수형에서도 그렇다고 생각합니다. – Digvijay

관련 문제