2014-09-23 52 views
0

嘗試在EF中添加遷移時遇到問題。該錯誤是關係約束中的從屬角色和主角色中的屬性數量必須相同

RegisterCountLog_Register_Target_RegisterCountLog_Register_Source:在一個關係約束的從屬和主角色屬性的數目必須是相同的。

的類如下:

public class RegisterCountLog 
{ 
    [ForeignKey("CountLog")] 
    public long DeviceSerial { get; set; } 

    [Key, Column(Order = 2)] 
    [ForeignKey("CountLog")] 
    public long LogEntryID { get; set; } 

    [Key, Column(Order = 3)] 
    [ForeignKey("Register")] 
    public long RegisterId { get; set; } 

    public long Value { get; set; } 

    public virtual CountLog CountLog { get; set; } 

    public virtual Register Register { get; set; } 
} 

public class Register 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    [Key, Column(Order = 1)] 
    public long RegisterId { get; set; } 

    [Key, ForeignKey("Device"), Column(Order = 2)] 
    public long DeviceSerial { get; set; } 

    [StringLength(50)] 
    public string RegisterName { get; set; } 

    public ContributionType Contribution { get; set; }  

    public virtual Device Device { get; set; } 

    public virtual ICollection<RegisterCountLog> CountLogs { get; set; } 
} 

誰能幫助?

回答

0

因此註冊模型有合成密鑰RegisterId, DeviceSerial,您必須在RegisterCountLog模型中指定兩個密鑰。

[ForeignKey("RegisterId ,DeviceSerial")] 
public virtual Register Register { get; set; } 
+0

這有什麼不同? – Chris 2014-09-23 11:10:23

+0

答案已經更新。 – 2014-09-23 11:27:06

相關問題