【问题标题】:The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical关系约束中的 Dependent 和 Principal Roles 中的属性数量必须相同
【发布时间】:2014-11-17 13:23:05
【问题描述】:

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

谁能帮忙?

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    因此注册模型有一个组合键RegisterIdDeviceSerial,您必须在RegisterCountLog 模型中指定这两个键。

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

    【讨论】:

    • 这有什么区别?
    • 当你定义一个外键时,你必须有一种方法来检索你正在引用的外部表。您不能只用一个键检索 Register 表,因为它有 2 个 PK(1 个 PK 不唯一地定义 Register 类)。因此必须清楚地说明foreign table的所有PK。
    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 2012-07-30
    • 2017-06-13
    • 2012-10-31
    • 2020-10-29
    • 1970-01-01
    • 2019-12-26
    相关资源
    最近更新 更多