【问题标题】:Navigation Property other than primary key in self referencing table自引用表中除主键外的导航属性
【发布时间】:2019-11-26 09:35:32
【问题描述】:

我有一个名为Customer 的表,它有两个属性ReferrerCodeOwnReferrerCode。客户注册时,可以输入另一个客户的推荐人代码,系统将分配一个唯一的OwnReferrerCode。多个客户可以使用特定客户的推荐代码。

public class Customer 
{
  [PrimaryKey]
  public long CustomerId {get;set;}
  public string ReferrerCode {get;set;}
  public string OwnReferrerCode {get;set;}

  public Customer ReferrerCustomer { get; set; }
  public ICollection<Customer> ReferredCustomers { get; set; }
}

在配置文件中:

this.HasOptional<Customer>(s => s.ReferrerCustomer).WithMany(g => g.ReferredCustomers)
                .HasForeignKey(s => s.ReferrerRewardCode);

显然会返回这个错误 The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role.

有没有办法在 EF6

中建立这种关系

【问题讨论】:

    标签: entity-framework entity-framework-6 one-to-many


    【解决方案1】:

    正如您所解释的,客户实体可能有一个引荐客户,因此该关系类似于父子关系。 child-parent optional relationship entity-framework

    【讨论】:

    • 在这个链接中,解释了当父子关系与PrimaryKey。但就我而言,我必须更改对我来说不可行的模型类。
    • 您应该删除ReferrerCode,而是使用ReferrerCustomerId 作为引用ReferrerCustomer(子父关系)的可选外键。这是正确的设计,因为您正在制作重复字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 2011-08-07
    • 2011-08-09
    相关资源
    最近更新 更多