无法确定外键的复合外键排序

时间:2016-07-04 16:10:29

标签: c# entity-framework data-annotations

我在两个模型之间建立关系时遇到问题。表中有3个复合键。当我尝试将Category.Code链接到Service.CategoryCode时,会发生此问题。如果我在两个表中使用相同的列名,EF就能够处理。但我不想使用Category.CategoryCode。我是EF的新手。请帮助我如何通过注释和/或流畅的api实现这一点。

public class Category
{
    public Category()
    {
        this.Services= new HashSet<Service>();
    }
    [Key, Column(Order = 0)]
    public string ApplicationCode { get; set; }
    [Key, Column(Order = 1)]
    public string CompanyCode { get; set; }
    [Key, Column(Order = 2)]
    public string Code { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Service> Services { get; set; }
}

public class Service
{
    [Key, Column(Order = 0)]
    [ForeignKey("Category")]
    public string ApplicationCode { get; set; }
    [Key, Column(Order = 1)]
    [ForeignKey("Category")]
    public string CompanyCode { get; set; }
    [Key, Column(Order = 2)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }
    [ForeignKey("Category")]
    public string CategoryCode { get; set; }
    public string Description { get; set; }
    public string Remarks { get; set; }

    public virtual Category Category { get; set; }
}

1 个答案:

答案 0 :(得分:0)

哦,我认为回答你的问题为时已晚。 我正在使用“订单”来建立许多关系。 例如:

public class Subscript
{
    [Key, Column(Order = 0)]
    public long ID { get; set; }

    [ForeignKey("Customer")]
    public int CustomerId { get; set; }

    public string ChatType { get; set; }

    [ForeignKey("Channel")]
    public int? ChannelId { get; set; }
    public virtual Channel Channel { get; set; }

    [ForeignKey("Watermark") ,Column(Order = 1)]
    public int? Watermark_Id { get; set; }

    [ForeignKey("Watermark"), Column(Order = 2)]
    public string Watermark_Title { get; set; }

    public virtual Watermark Watermark { get; set; }

}

public class Watermark
{
    [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Key, Column(Order = 1, TypeName = "NVARCHAR")]
    [StringLength(100)]
    public string Title { get; set; }
}