21

RC 버전의 EF 4.1을 사용하여 빠른 ASP.NET MVC 3 응용 프로그램을 만들려고합니다.EF 4.1 - 모델 관계

public class Race 
{ 
    public int RaceId { get; set; } 
    public string RaceName { get; set; } 
    public string RaceDescription { get; set; } 
    public DateTime? RaceDate { get; set; } 
    public decimal? Budget { get; set; } 
    public Guid? UserId { get; set; } 
    public int? AddressId { get; set; } 

    public virtual Address Address { get; set; } 
} 

public class Address 
{ 
    public int AddressId { get; set; } 
    public string Street { get; set; } 
    public string StreetCont { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 

    public virtual Race Race { get; set; } 
} 

나는 새로운 인종을 삽입하려고 다음과 같은 오류가 발생합니다 : : 저는 두 가지 모델이

사이의 관계의 주요 끝 을 확인할 수 없습니다 'rcommander.Models.Race'및 'rcommander.Models.Address'입니다. 이 연결의 주 끝 부분 은 유창한 API 또는 데이터 주석 중 하나를 사용하여 명시 적으로 구성되어야합니다 ( ).

RaceId를 Races 테이블의 기본 키로 인식하고 AddressId를 FK to Addresses 테이블로 자동 인식해야합니까? 내가 놓친 게 있니?

감사합니다.

답변

21

에 여기서 문제는 EntityFramework가 두 객체 모두에서 상호 참조를 보유하고 있기 때문에 foreing 키가 어디에 있는지 인식 할 수없는 것 같습니다.

public class Race 
{ 
    public int RaceId { get; set; } 
    public string RaceName { get; set; } 
    public string RaceDescription { get; set; } 
    public DateTime? RaceDate { get; set; } 
    public decimal? Budget { get; set; } 
    public Guid? UserId { get; set; } 

    public int? AddressId { get; set; } 
    public virtual Address Address { get; set; } 
} 

public class Address 
{ 
    public int AddressId { get; set; } 
    public string Street { get; set; } 
    public string StreetCont { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 
} 

건너 뛰는 참조 2 실체에 경주 : 당신이 무엇을 달성하고자하는 확인되지 않는, 나는 이런 식으로 뭔가를 제안 할 수 있습니다.

+0

그게 전부 였어. 감사. – Mike

+4

주소 검색 기능이 제거되었습니다. –

+1

네, 답장에서 설명한대로 주소에서 네비게이션의 위치를 ​​제거하고 일대 다 관계로 매핑합니다. –

4

규칙에 따라 Id를 기본 키로 인식합니다. 그래서 당신이해야 할 일 :

public class Race 
{ 
    [Key] 
    public int RaceId { get; set; } 
    public string RaceName { get; set; } 
    public string RaceDescription { get; set; } 
    public DateTime? RaceDate { get; set; } 
    public decimal? Budget { get; set; } 
    public Guid? UserId { get; set; } 
    public int? AddressId { get; set; } 

    public virtual Address Address { get; set; } 
} 
and 

public class Address 
{ 
    [Key] 
    public int AddressId { get; set; } 
    public string Street { get; set; } 
    public string StreetCont { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 

    [ForeignKey("RaceId")] // Maybe telling it what the ForeignKey is will help? 
    public virtual Race Race { get; set; } 
} 

[Key] 속성은 당신이 원하지 않는 경우, 당신은 당신의 기본 키의 이름을 변경하는 데 필요한 기본 키

해야 함을 나타냅니다 단순히 public int Id {get; set; }

+1

같은 오류 중 하나 방법을 사용하여. 문제가 발생하면 Global.asax에서 Application_Start 내 DB Initializer를 DropCreateDatabaseIfModelChanges로 가져 왔습니다. – Mike

+1

음 ... 내 업데이트를 확인하십시오. ForeignKeyAttribute가 필요한 것일 수 있습니까? –

+1

IsKey InverseProperty ("RaceId") ForeignKey가 아닌 여기에 필요한 것은 무엇입니까? – Joe

18

여기 주소와 경주 사이에 1 : 1 관계가 있습니다. 당신은 아마 1로 매핑 할 : N 그래서 당신은에 주소를 수정해야합니다 외래 키해야 하나 다음 주소에서 AddressId 레이스에서하지만 AddressId을 사용할 수 없습니다 : 당신이 1 사용할 경우

public class Address 
{ 
    public int AddressId { get; set; } 
    public string Street { get; set; } 
    public string StreetCont { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 

    public virtual ICollection<Race> Races { ... } 
} 

왜냐하면 엔티티 프레임 워크는 1 : 1만이 "공유"기본 키일 수 있기 때문입니다.

+3

공유 기본 키를 사용하여 1 : 1을 사용하는 방법을 보여주기 위해 답변을 업데이트 할 수 있습니까? Race.Address와 Address.Race와 같이 액세스 할 수있는 양방향 방식이 필요합니다. 가능한 경우 인종과 주소를 유지하십시오. 감사. –

+1

@Leniel :이 답변을 확인하십시오 : http://stackoverflow.com/questions/5388077/primary-foreign-key-in-entity-framework/5388658#5388658 나는 그것이 당신이 찾고있는 것이라고 생각합니다. –

5

좋은 포스트가있다 : EF 코드 먼저 CTP5에서 협회 : 제 2 부 - 공유가 기본 키 협회

http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx

+0

"우리 협회를 일대일로 바꾸는 한 가지 방법은 양방향으로 만드는 것입니다. 즉, User 유형의 Address 클래스와 Shipment 유형 중 하나에 새로운 탐색 속성을 추가하는 것입니다. -이 방법은 효과가없는 것 같습니다. . 나는 "유형 간의 연관성의 주된 결론을 결정할 수 없습니다.이 연관성의 주된 목적은 유창한 API 또는 데이터 주석의 관계를 사용하여 명시 적으로 구성되어야합니다." – Karan

8

이 일대일 관계를 들어, 당신은 추가해야 "[필수]는" 속성을 두 번째 클래스에 추가합니다. 아래를 참조

public class Race 
{ 
    public int RaceId { get; set; } 
    public string RaceName { get; set; } 
    public string RaceDescription { get; set; } 
    public DateTime? RaceDate { get; set; } 
    public decimal? Budget { get; set; } 
    public Guid? UserId { get; set; } 

    public int? AddressId { get; set; } 
    public virtual Address Address { get; set; } 
} 

public class Address 
{ 
public int AddressId { get; set; } 
public string Street { get; set; } 
public string StreetCont { get; set; } 
public string City { get; set; } 
public string State { get; set; } 
public string ZipCode { get; set; } 

[required] 
public Race Race { get; set; } 

} 
0

내가 그것을 이렇게도 해결 될 것이라고 생각 ... 나는 주소가 인종과 관련이 필요하지 않은 것으로 가정하지만, 경주는 항상 주소와 연결되어 있어야합니다. 나는 환자 및 사고와 같은 문제를했고 내가 실제로 외래 키와 동일 InverseProperty으로 해결하지만, 다른 방향

public class Race 
{ 
    public int RaceId { get; set; } 
    public string RaceName { get; set; } 
    public string RaceDescription { get; set; } 
    public DateTime? RaceDate { get; set; } 
    public decimal? Budget { get; set; } 
    public Guid? UserId { get; set; } 

    public int AddressId { get; set; } 

    [ForeignKey("AddressId")] 
    public virtual Address Address { get; set; } 
} 

public class Address 
{ 
public int AddressId { get; set; } 
public string Street { get; set; } 
public string StreetCont { get; set; } 
public string City { get; set; } 
public string State { get; set; } 
public string ZipCode { get; set; } 

public int? RaceId { get; set; } 
[InverseProperty("RaceId")] 
public Race Race { get; set; } 

}