Entity framework EF core具有相同外键的多个虚拟属性

Entity framework EF core具有相同外键的多个虚拟属性,entity-framework,entity,Entity Framework,Entity,在实体框架中,是否可以对多个realted属性使用相同的外键。例如: // There is one table for credit cards. To destinguish between company credit cards and lets say shoppers credit cards there is tag field CustomerType. SO two different credit cards can have the smae EntityId but if

在实体框架中,是否可以对多个realted属性使用相同的外键。例如:

// There is one table for credit cards. To destinguish between company credit cards and lets say shoppers credit cards there is tag field CustomerType. SO two different credit cards can have the smae EntityId but if CustomerType is different the navigatin property would point to either Client or Company table.
public enum CustomerType
{
   Client,
   Company
}

public class Client
{
   public int Id { get; set; }
   virtual public IEnumerable<CreditCard> CreditCards { get; set; }  
}
public class Company
{
    public int Id { get; set; }
    virtual public IEnumerable<CreditCard> CreditCards { get; set; }  
}

public class CreditCard
{
   public int Id { get; set; }
   //this points to either company or client depending on the customertype field.
   public int EntityId { get; set; }
   public CustomerType Type { get;set;}
   public virtual Client Client { get; set; }
   public virtual Company Company { get; set; }
}
 ......
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   // obviously this is wrong ...
   modelBuilder.Entity<Client>(entity =>
   {
       entity.HasMany(x => x.CreditCards)
           .WithOne(y => y.Client)
           .HasForeignKey(z => z.HolderId);
   });

   modelBuilder.Entity<Company>(entity =>
   {
      entity.HasMany(x => x.CreditCards)
          .WithOne(y => y.Company)
          .HasForeignKey(z => z.HolderId);
   });
 }
//有一张信用卡表。要在公司信用卡和购物者信用卡之间取消语言,有标记字段CustomerType。因此,两张不同的信用卡可以具有smae EntityId,但如果CustomerType不同,NavigateIn属性将指向Client或Company表。
公共枚举CustomerType
{
客户
单位
}
公共类客户端
{
公共int Id{get;set;}
虚拟公共IEnumerable信用卡{get;set;}
}
公营公司
{
公共int Id{get;set;}
虚拟公共IEnumerable信用卡{get;set;}
}
公营信用卡
{
公共int Id{get;set;}
//根据customertype字段,这指向公司或客户。
public int EntityId{get;set;}
公共CustomerType类型{get;set;}
公共虚拟客户端{get;set;}
公共虚拟公司公司{get;set;}
}
......
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
//显然这是错误的。。。
modelBuilder.Entity(Entity=>
{
entity.HasMany(x=>x.CreditCards)
.WithOne(y=>y.Client)
.HasForeignKey(z=>z.HolderId);
});
modelBuilder.Entity(Entity=>
{
entity.HasMany(x=>x.CreditCards)
.有一家(y=>y.公司)
.HasForeignKey(z=>z.HolderId);
});
}

或者我应该忘记它,把公司信用卡和客户信用卡放在不同的表格里。这将是非常严重的。

该模型打破了标准化。从表结构来看,如果公司信用卡与客户信用卡有单独的表,那么实体也应该声明公司信用卡与客户信用卡。在建议的表结构中,每个具体继承的表与您似乎希望在实体中设置的每个层次结构的表相混淆。通常,最好让您的实体镜像您的数据结构

EF可以通过继承处理此场景:

public abstract class CreditCard
{
    [Key]
    public int Id { get; set; }
    // card details
}

public class ClientCreditCard : CreditCard
{
    public virtual Client Client { get; set; }
}

public class CompanyCreditCard : CreditCard
{
    public virtual Company Company { get; set; }
}
该公司将拥有一系列公司信用卡,而客户将拥有客户信用卡

从表的角度来看,您可以有一个带有信用卡类型鉴别器的单一信用卡表,尽管它对公司或客户都有可为空的ID。(每个层次结构的表)以维护与其他实体的FK关系。具有一个鉴别器+“EntityId”,它将指向或破坏规范化。(不可能有FK)