实体框架6 - >将一个属性用于两个复合外键

时间:2018-02-19 15:21:02

标签: c# entity-framework

背景:我们正在接收来自外部系统的数据,这些数据将存储在我们的数据库中,模型基于我们从中获取的信息。

除了一个使用一个属性作为两个不同关系的复合外键的模型外,一切都运行顺利。

以下代码会生成错误Duplicate 'ForeignKey' attribute。如果我从ForeignKey中删除其中一个BusinessSystemId属性,我会收到错误The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.我认为代码清楚地显示了我要实现的目标。那就是使用BusinessSystemId作为TPCaseName中的两个单独的外键。

public class TPCase
{
    [Key, Column(Order = 0)]
    public string BusinessSystemId { get; set; }

    [Key, Column(Order = 1)]
    public int CaseId { get; set; }

    public virtual ICollection<TPCaseName> CaseNames { get; set; }
}

public class TPCaseName
{
    [Key, Column(Order = 0)]
    [ForeignKey("TPCase")]
    [ForeignKey("TPName")]
    public string BusinessSystemId { get; set; }

    [Key, Column(Order = 1)]
    [ForeignKey("TPCase")]
    public int CaseId { get; set; }

    [InverseProperty("CaseNames")]
    public virtual TPCase TPCase { get; set; }

    [Key, Column(Order = 2)]
    [ForeignKey("TPName")]
    public int NameNo { get; set; }

    public virtual TPName TPName { get; set; }

    [Key, Column(Order = 3)]
    [ForeignKey("TPNameType")]
    public string NameType { get; set; }

    public virtual TPNameType TPNameType { get; set; }

    public string ContactName { get; set; }
}

public class TPName
{
    [Key, Column(Order = 0)]
    public string BusinessSystemId { get; set; }

    [Key, Column(Order = 1)]
    public int NameNo { get; set; }

    public string NameCode { get; set; }
}

我可以通过向TPCaseName添加以下属性来解决此问题,但由于BusinessSystemId中已存在信息,因此似乎没有必要。

[ForeignKey("TPName"), Column(Order = x)]
public string BusinessSystemId2 { get; set; }

我尝试使用相同的方案搜索问题,但没有找到任何问题。

1 个答案:

答案 0 :(得分:1)

您只需要注释导航属性以指定复合外键。 EG

        public class TPCase
        {
            [Key, Column(Order = 0)]
            public string BusinessSystemId { get; set; }

            [Key, Column(Order = 1)]
            public int CaseId { get; set; }

            public virtual ICollection<TPCaseName> CaseNames { get; set; }
        }

        public class TPCaseName
        {
            [Key, Column(Order = 0)]
            public string BusinessSystemId { get; set; }

            [Key, Column(Order = 1)]
            public int CaseId { get; set; }

            [Key, Column(Order = 2)]
            public int NameNo { get; set; }

            [Key, Column(Order = 3)]
            public string NameType { get; set; }


            [ForeignKey("BusinessSystemId,CaseId")]
            public virtual TPCase TPCase { get; set; }

            [ForeignKey("BusinessSystemId,NameNo")]
            public virtual TPName TPName { get; set; }

            //[ForeignKey("NameType")]
            // public virtual TPNameType TPNameType { get; set; }
            public string ContactName { get; set; }
        }

        public class TPName
        {
            [Key, Column(Order = 0)]
            public string BusinessSystemId { get; set; }

            [Key, Column(Order = 1)]
            public int NameNo { get; set; }

            public string NameCode { get; set; }
        }