2012-01-17 63 views
0

我這裏有2個實體創建2個相同的外鍵

讓利說

飛行 flight_id - PK 起源 - FK1 目的地 - FK2

國家 COUNTRY_ID - PK 國家 代碼

樣本代碼 類別F1 ight { public int ID {get;組; }

[BelongsTo(Column = "Origin", ForeignKey = "country_id")] 
    public Countries Origin {get; set;} 

    [BelongsTo(Column = "destination", ForeignKey = "country_id")] 
    public Countries Destination {get; set; } 

}

創建於ActiveRecord的架構時,我得到一個錯誤。什麼可以替代這個?謝謝!

回答

2

您正在命名BelongsTo屬性中的ForeignKey參數對於這兩個鍵都是相同的。該參數不是要使用的列的名稱,但實際上是ActiveRecord在創建模式時使用它命名的約束的名稱。

我做了一些假設,並與工作的例子擴充您的代碼示例:

[ActiveRecord] 
public class Flight 
{ 
    [PrimaryKey] 
    public int Id { get; set; } 

    [BelongsTo(Column = "Origin", ForeignKey = "country_id_origin")] 
    public Countries Origin { get; set; } 

    [BelongsTo(Column = "Destination", ForeignKey = "country_id_destination")] 
    public Countries Destination { get; set; } 
} 

[ActiveRecord] 
public class Countries 
{ 
    [PrimaryKey] 
    public int country_id { get; set; } 

    [Property] 
    public string CountryName { get; set; } 
}