Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何在使用每类型表继承的表之间定义EF多对多关系?_Entity Framework - Fatal编程技术网

Entity framework 如何在使用每类型表继承的表之间定义EF多对多关系?

Entity framework 如何在使用每类型表继承的表之间定义EF多对多关系?,entity-framework,Entity Framework,我定义了以下模型: public class Account { public int Id {get; set;} ... // other account properties public ICollection<AccountAddress> Addresses {get; set;} } public class Address { public int Id {get; set;} public string Street1 {get

我定义了以下模型:

public class Account {
   public int Id {get; set;}
   ... // other account properties

   public ICollection<AccountAddress> Addresses {get; set;}
}

public class Address {
    public int Id {get; set;}
    public string Street1 {get; set;}
    public string Street2 {get; set;}
    ... // other standard address properties
}

public class AccountAddress : Address {
  public int AccountId {get; set;}
  public DateTime? BeginDate {get; set;}
  public string Notes {get; set;}
  ... // other account address specific properties
}

public class Site {
  public int Id {get; set;}
  public Address SiteAddress {get; set;}
}
有时为错误的帐户提供扩展属性

我需要做什么才能让它工作

编辑: 虽然接受的答案没有具体回答我的问题(即,我如何定义两个实体之间的多对多关系,其中一个实体是用TPT继承映射定义的),但它确实为提出可接受的解决方案提供了一些帮助

我确实放弃了实体继承,只是简单地将地址添加为AccountAddress实体的属性。然后,我给AccountAddress(addressextended table)提供了它自己的主id。我将accountaddresses表更改为包含account id和AccountAddress id(addressextended table的新id)的联接表


我还必须为地址的AccountAddress添加一个导航属性,并随之添加一个映射。

我相信您可以通过更改查看“AccountAddress”和“SiteAddress”表的方式来解决此问题。与其将它们作为Address的子类,不如将它们设置为“联接表”。AccountAddress实体连接了一个地址和一个帐户,因此从技术上讲,它必须拥有一个帐户和一个地址。您还可以指定将地址与帐户链接时相关的其他信息(例如地址类型、清除日期等)

据我所知,您试图实现的目标是将多个地址链接到多个帐户(或站点),并保留有关每个链接的一些元数据

public class Account {
   public int Id {get; set;}
   ... // other account properties

   public ICollection<AccountAddress> Addresses {get; set;}
}

public class Address {
    public int Id {get; set;}
    public string Street1 {get; set;}
    public string Street2 {get; set;}

    public ICollection<AccountAddresses> Accounts {get; set;} // this denotes your two-way connection with the Account entity
    ... // other standard address properties
}

public class AccountAddress {
  public int AccountId {get; set;}
  public virtual Account {get; set;} // these are your "Account" properties

  public int AddressId {get; set;}
  public virtual Address {get;set;} //these are your "Address" properties

  public DateTime? BeginDate {get; set;}
  public string Notes {get; set;}
  ... // other account address specific properties
}

Address是一种更适合作为复杂类型而不是完整实体的类型。主要原因是地址实体从未被重复使用:关系可能是1:n,但实际上它们总是1:1。这是因为在运行时定义关系的逻辑不会与您从中选择正确地址的现有地址列表一起工作(因此您可以重用地址),而是使用用户的输入,因此您将始终创建一个新的地址实体实例

当从zipcode表中获取地址时,无论如何都不需要存储它们,因此如果存储地址信息,则不需要将其存储为实体

m:n与“地址”的关系在实践中永远不会被使用。因此,重构实体,将Address用作实体内部的复杂类型。如果需要不同的地址类型,请为不同的地址类型定义不同的复杂类型。数据模型中的继承是您无论如何都要避免的,除非您计划在运行时通过多态性重新使用验证逻辑,否则它实际上并没有那么好:性能会受到影响,您的模型实际上会更复杂。如果您希望有一个通用接口来与通用地址信息进行通信,请定义IAddress并在您的复杂地址类型上实现它


So TL;DR:把地址定义为实体类型,把它们定义为复杂类型,去掉继承,这对你一点帮助都没有

Account
AccountAddress
之间的关系是多对多(这意味着关系的FK在链接表
AccountAddress
中)时,
AccountId
的用途是什么?我可能没有很好地解释这个问题。这个想法是,当一个帐户绑定到任何地址时,我需要跟踪有关该帐户/地址关系的特定细节。它们不属于地址表,也不能(据我所知)放在链接表中。例如,帐户123和帐户456都可以绑定到地址40。但是,对于账户123,该地址有一种工作地址类型,而账户456的地址40作为邮寄地址或带有清除日期的临时地址。Jeric,感谢您的输入。看来你的建议应该行得通;但是,它确实会使地址有点混乱。照此看来,并不是每个地址都与一个帐户绑定,或者都有帐户信息。我试图使示例保持简单,但实际上,还有许多其他类与地址相关,它们可能有自己的“扩展”属性。因此,联系人地址可能有地址类型,但没有开始日期、清除日期或主标志。站点地址没有扩展属性。不过,我的想法是能够跨这些不同的实体重用实际地址。我花了一点时间再次找到这个。我以前就用过这个答案(或类似的答案)。嗯,我不确定我是否完全同意。我们有1000多名会员,所有会员都有账户(有多个地址)。我们有几百个办公地点(每个都有一个或多个地址)。我们的大多数会员(账户)最终将绑定到我们的一个办公地址。从外观上看,用户界面将允许他们从我们的办公地点列表中进行选择,或为个人输入新地址。当一个教会有一个地址,而我们的一个成员在教会外工作时(因此他的工作地址就是教会地址),这一点就更进一步了。我们不断重复使用地址。我试图做的另一件事是在代码中重复使用地址验证,并定义地址编辑模板,以便在使用地址的3-5个区域中,我可以使用地址编辑模板,而不是重新定义地址的所有逻辑(即,选择一个国家并过滤州/省列表等)。
var _account = _context.Accounts
   .Include(a => a.Addresses)
   .SingleOrDefault(a => a.Id = 1234);
public class Account {
   public int Id {get; set;}
   ... // other account properties

   public ICollection<AccountAddress> Addresses {get; set;}
}

public class Address {
    public int Id {get; set;}
    public string Street1 {get; set;}
    public string Street2 {get; set;}

    public ICollection<AccountAddresses> Accounts {get; set;} // this denotes your two-way connection with the Account entity
    ... // other standard address properties
}

public class AccountAddress {
  public int AccountId {get; set;}
  public virtual Account {get; set;} // these are your "Account" properties

  public int AddressId {get; set;}
  public virtual Address {get;set;} //these are your "Address" properties

  public DateTime? BeginDate {get; set;}
  public string Notes {get; set;}
  ... // other account address specific properties
}
modelBuilder.Entity<Account>().HasMany(a => a.AccountAddresses)
                              .WithRequired(aa => aa.Account)
                              .HasForeignKey(aa => aa.AccountId);

modelBuilder.Entity<Address>().HasMany(a => a.Accounts)
                              .WithRequired(ac => ac.Address)
                              .HasForeignKey(ac => ac.AddressId);
var _account = _context.Accounts
   .Include(a => a.Addresses)
   .Include(a => a.Addresses.Address)
   .SingleOrDefault(a => a.Id = 1234);