我使用ASP.NET MVC 3與實體框架4代碼的第一種方法,每次我嘗試使用模型上的鍵屬性指定複合鍵時,我會得到這個錯誤:實體框架複合鍵代碼首先導致模型驗證錯誤
System.Data.Edm.EdmAssociationConstraint: : Number of Properties in the Dependent and Principal Role in a relationship constraint must be exactly identical.
我使用的列屬性來區分主鍵像這樣的順序:
public class Game
{
[Key, Column(Order=0)]
public Guid GameId { get; set; }
[Key, Column(Order=1)]
public string Name { get; set; }
public string Description { get; set; }
public Game()
{
this.GameId = Guid.NewGuid();
}
}
我想知道是否有另一種方法來創建組合鍵,或也許有辦法阻止這個錯誤?我知道可以向OnModelBuild事件添加邏輯,但如果可能的話,我寧願使用模型上的關鍵屬性。
看起來您正在使用關聯屬性(GameId?)之一。 Game有其他實體有什麼關聯? –
我正在構建一個遊戲數據庫,其中包含來自Xbox Live,PSN和Steam的遊戲。遊戲實體是這些網絡遊戲中每一個遊戲的超級類別。此外,我將使用遊戲實體來表示不特定於在線網絡以及SNES或NES等較舊遊戲機的遊戲。 –