2012-01-26 6 views
1

나는 작업 모델을 가지고 있지만 관계가 데이터베이스에 두 번 생성되었음을 알았습니다. 원래는 테이블에 두 개의 열을 만들었지 만 지정된 외래 키 특성이 추가되어 이제는 외래 키 특성이 추가되었습니다.코드 첫 번째 EF 관계 중복

저는 계정을 사용할 수있는 고용주가 많은 계정 클래스가 있습니다. (일대)는 여기에 클래스입니다 :

여기
public class Account 
{ 
    public int AccountId { get; set; } 

    [Required(ErrorMessage = Constants.ValidationMessages.FieldRequired)] 
    public string Name { get; set; } 

    public int? PrimaryUserId { get; set; } 
    [ForeignKey("PrimaryUserId")] 
    public Employer PrimaryUser { get; set; } 

    [ForeignKey("EmpAccountId")] 
    public ICollection<Employer> Employers { get; set; } 

} 

상속 된 고용주 클래스에게 있습니다

public class Employer : User 
{ 
    public Employer() 
    { 
     DepartmentsToPost = new Collection<Department>(); 
     Contacts = new Collection<Contact>(); 
    } 

    [Display(Name = "Workplaces to advertise jobs")] 
    public virtual ICollection<Department> DepartmentsToPost { get; set; } 

    public int EmpAccountId { get; set; } 
    [ForeignKey("EmpAccountId")] 
    public virtual Account Account { get; set; } 

    public override string UserType 
    { 
     get { return "Employer"; } 
    } 
} 

사용자 테이블 :이

UserId 
Username 
FirstName  
Surname 
EmpAccountId 
Discriminator 

계정 표

AccountId 
Name 
PrimaryUserId 

User 테이블에 대한 하나의 링크입니다. 이 필드는 PrimaryUser 필드이며 올바른 내용입니다. 다른 두 가지 관계가 있습니다 : 계정 -> 고용주. EF는 그것들을 Account_EmployersEmployer_Account이라고 명명했습니다. 이것들은 중복됩니다.

어떻게 이런 일이 일어나지 않도록 할 수 있습니까?

답변

2

컬렉션은 다른 쪽의 탐색 속성을 가리 키도록 InversePropertyAttribute으로 꾸며야합니다.

public class Account 
{ 
    public int AccountId { get; set; } 

    [Required(ErrorMessage = Constants.ValidationMessages.FieldRequired)] 
    public string Name { get; set; } 

    public int? PrimaryUserId { get; set; } 
    [ForeignKey("PrimaryUserId")] 
    public Employer PrimaryUser { get; set; } 

    [InverseProperty("Account")] 
    public ICollection<Employer> Employers { get; set; }  
} 
+0

빠른 답변 감사드립니다. 나는 그걸 시험으로 만했다. 내 연구 결과가 옳은 대답으로 확인 된 것을 기쁘게 생각합니다! – Simon

+0

Simon, 대답이 좋다면 그것을 받아들이는 것이 아니라 그것을 upvoting하는 것을 고려하십시오. –

+0

나는 평판이 15가 될 때까지 upvote 할 수 없다. .. 그렇지 않으면 나는 매우 좋아할 것이다. 죄송합니다! – Simon