关系约束中的从属角色和主要角色中的属性数必须相同

时间:2014-09-23 11:00:11

标签: c# entity-framework

尝试在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; }
}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

因此,注册模型具有复合键RegisterId, DeviceSerial,您必须在RegisterCountLog模型中指定两个键。

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