EF Core 2.1 foreign key with different datatype

Viewed 65

I have thes model classes:

public partial class InventoryCustomer : EntityBase, ICrudEntity<int>
{
    public int Id { get; set; }
    public int? SlsalesLevelTypeId { get; set; }
    public long? SalesLevelId { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual GroupOfCustomer GroupOfCustomer { get; set; }
}

public partial class Customer : EntityBase, IPkEntity<long>
{
    public long Id { get; set; }
    public virtual ICollection<InventoryCustomer> InventoryCustomer { get; set; }
}

public partial class GroupOfCustomer : EntityBase, ICrudEntity<int>
{
    public int Id { get; set; }
    public virtual ICollection<InventoryCustomer> InventoryCustomer { get; set; }
}

Property Id must remain an int at GroupsOfCustomer and long at Customer.

Depends of SlsalesLevelTypeId, SalesLevelId at InventoryCustomer references either on Customer or GroupOfCustomer.

Map class look like this:

 public class InventoryCustomerMap : EntityTypeConfiguration<InventoryCustomer>
 {
     public override void Map(EntityTypeBuilder<InventoryCustomer> builder)
   {
    builder.ToTable("InventoryCustomers");

    builder.HasKey(e => e.Id);

    builder.Property(e => e.SalesLevelId).HasColumnName("SalesLevelID");

    builder.HasOne(d => d.Customer)
        .WithMany(p => p.InventoryCustomer)
        .HasForeignKey(d => d.SalesLevelId)
        .HasPrincipalKey(p => p.Id);

    builder.HasOne(d => d.GroupOfCustomer)
          .WithMany(p => p.InventoryCustomer)
          .HasForeignKey(d => d.SalesLevelId)
          .HasPrincipalKey(p => p.Id);
    }
}

But when I run code, I get an error:

enter image description here

0 Answers
Related