0

I have a table named Customer and it has two properties ReferrerCode and OwnReferrerCode. When a customer registers, he/she can put a referrer code of another customer and the system will assign a unique OwnReferrerCode. Multiple customers can use a specific customer's refer code.

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; }
}

And in config file:

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

It obviously returns this error 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.

Is there any way to do that kind of relation in EF6

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Moshi
  • 1,385
  • 2
  • 17
  • 36

1 Answers1

0

As you explained, the customer entity may have one referrer customer, so the relation is like a child-parent relation. child-parent optional relationship entity-framework

Amir Jelo
  • 347
  • 1
  • 7
  • In this link, it is explained when a parent-child relationship with `PrimaryKey`. But in my case, I have to change model class which is not feasible for me. – Moshi Nov 26 '19 at 12:34
  • 1
    You should remove the `ReferrerCode` and instead, use `ReferrerCustomerId` as an optional foreign key that refers to the ReferrerCustomer (child-parent relationship). This is the right design because you are making a repeative field. – Amir Jelo Nov 27 '19 at 05:04