21

我試圖創建一個使用EF 4.1 RC版快速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作爲競賽表的主鍵和AddressId爲FK自動將地址表?我錯過了什麼嗎?

謝謝!

回答

21

這裏的問題似乎是EntityFramework無法識別前面的鍵,因爲您在兩個對象中都持有交叉引用。不是確定要達到什麼樣的,我建議是這樣的:

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; } 
} 

跳過參考第二實體的比賽。

+0

就是這樣。謝謝。 – Mike 2011-03-18 16:15:05

+4

不會這從地址刪除導航posibilities雖然? – 2011-03-18 16:31:35

+1

是的,它會從地址中刪除導航可能性並將其映射爲一對多關係,如我在答案中所述。 – 2011-03-18 18:34:21

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]屬性表明,它應該是PrimaryKey的

如果你不希望這樣,你需要將自己的主鍵簡單地public int Id {get; set; }

+1

使用同樣的錯誤這兩種方法。如果有問題,我在Global.asax的Application_Start中將我的數據庫初始值設定項設置爲DropCreateDatabaseIfModelChanges。 – Mike 2011-03-18 12:47:21

+1

嗯...檢查我的更新..也許ForeignKeyAttribute是你需要什麼? – 2011-03-18 13:27:58

+1

Isnt InverseProperty(「RaceId」)這裏需要什麼,而不是ForeignKey? – Joe 2011-12-02 09:15:24

18

這裏的問題是1:地址和種族之間的關係1!你可能想將其對應爲1:N,所以你需要修改地址:

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,那麼你不能在比賽中,但在AddressId地址使用AddressId須爲外鍵因爲實體框架可以實現1:1只是「共享」主鍵。

+3

您可以請更新您的答案,以顯示如何使用共享主鍵1:1?我想有一個像這樣訪問的雙向方式:Race.Address和Address.Race。如果可能,請在您的示例中保持比賽和地址。謝謝。 – 2011-04-27 20:42:21

+1

@Leniel:檢查這個答案:http://stackoverflow.com/questions/5388077/primary-foreign-key-in-entity-framework/5388658#5388658我認爲這是你在找什麼。 – 2011-04-27 20:46:20

5
+0

「把我們的關聯變成一對一的一種方法是讓它們成爲雙向的,也就是將一個新的導航屬性添加到Address類型的User類型和另一個類型的Shipment類型。」 - 這似乎不起作用。我得到一個「無法確定類型之間關聯的主要端點......此關聯的主體端必須使用關係流利API或數據註釋來顯式配置」。 – Karan 2012-10-02 16:28:26

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; } 

}