简体   繁体   English

如何配置版本化实体之间的一对多/多对一关系

[英]How to configure a one-to-many/many-to-one relationship between versioned entities

I was given (therefore I cannot change it) a database with entities that are versioned using a version number column.我获得了(因此我无法更改它)一个数据库,其中包含使用version号列进行版本控制的实体。

The entities are Person , Domicile , Address .实体是PersonDomicileAddress Each of this entities has a composite primary key made with id and version.每个实体都有一个由 id 和 version 组成的复合主键。

A Person can have multiple Domiciles , a Domicile references an Address .一个可以有多个住所,一个住所引用一个地址 An Address can be referenced by different Domiciles .一个地址可以被不同的住所引用。 Therefore we have a one-to-many relationship between a Person and its Domiciles and there is a many-to-one relationship between Domicile and the Address .因此,我们在Person和它的Domiciles之间存在一对多的关系,在DomicileAddress之间存在多对一的关系。

Each entity reference the other entity only using the id, therefore every entity is versioned indipendently.每个实体仅使用 id 引用另一个实体,因此每个实体都是独立版本的。

Let me give you an example of some tables:让我举几个表的例子:

Person

|id|version|etc.
|1 |1      |...
|1 |2      |...

Domicile住所

|id|version|person_id|address_id|etc...
|2 |1      |1        |3         |...
|2 |2      |1        |3         |...
|3 |1      |1        |4         |...

Address地址

|id|version|etc...
|3 |1      |...
|3 |2      |...
|4 |1      |...

As you can see each entity reference each other using just the id, and there can be multiple versions of the same entity therefore you can have the same id multiple times.如您所见,每个实体仅使用 id 相互引用,并且同一实体可以有多个版本,因此您可以多次使用相同的 id。

I would like my model entities to look something like:我希望我的 model 实体看起来像:

public class Person {
  public long Id {get; set;}
  public long Version {get; set;}
  public List<Domicile> Domiciles {get; set;}
}

public class Domicile {
  public long Id {get; set;}
  public long Version {get; set;}
  public long AddressId {get; set;}
  public List<Address> AddressVersions {get; set;}
}

public class Address {
  public long Id {get; set;}
  public long Version {get; set;}
}

Configuring the relationship between a person and its domiciles it's straight-forward:配置一个人与其住所之间的关系很简单:

modelBuilder.Entity<Person>(entity => {
  //...
  entity.HasMany(p => p.Domiciles)
    .WithOne()
    .HasForeignKey(d => d.PersonId)
    .HasPrincipalKey(p => p.Id);
});

Now comes the part that I have no clue how to configure: the relationship between a Domicile and its Address versions.现在是我不知道如何配置的部分: Domicile与其地址版本之间的关系。 In theory this relation should be a many-to-one because one Address is used by multiple Domiciles .从理论上讲,这种关系应该是多对一的,因为一个Address被多个Domiciles使用。

I want to have ALL the Address versions on the Domicile so I've declared the property public List<Address> AddressVersions , but now I have to configure the navigation property.我想在 Domicile 上拥有所有地址版本,所以我已经声明了属性public List<Address> AddressVersions ,但现在我必须配置导航属性。

If I try to configure the relationship as:如果我尝试将关系配置为:

modelBuilder.Entity<Domicile>(entity => {
  //...
  entity.HasMany(p => p.AddressVersions)
    .WithMany());
});

This will not compile because a many-to-many relationship requires a navigation property on both sides.这不会编译,因为多对多关系需要双方都有导航属性。

If I try to model the relationship with a one-to-many configuration:如果我尝试将 model 与一对多配置的关系:

modelBuilder.Entity<Domicile>(entity => {
  //...
  entity.HasMany(p => p.AddressVersions)
    .WithOne()
    .HasPrincipalKey(p => p.AddressId);
});

It will not work because I don't have a foreign key on Address that refers to Domicile given that an Address is used by multiple Domiciles .它不起作用,因为我在Address上没有引用Domicile的外键,因为Address被多个Domiciles使用。

If I go the other way around with the relationship:如果我 go 与关系相反:

modelBuilder.Entity<Domicile>(entity => {
  //...
  entity.HasOne(p => p.Address)
    .WithMany()
    .HasForeignKey(p => p.AddressId)
    .HasPrincipalKey(d => d.Id);
});

Despite this works I get just one of the many possibile versions of an Address , losing information.尽管有这些工作,但我只得到了Address的许多可能版本之一,丢失了信息。

How can I model such relations like between Address and Domicile and making available on the principal entity all the versions of the related entity?我怎样才能 model地址住所之间的这种关系,并在主体实体上提供相关实体的所有版本?

Short answer is what you want cannot be done with built-in functionality.简短的回答是您想要的功能无法通过内置功能完成。 Perhaps third party extensions/tools could help.也许第三方扩展/工具可以提供帮助。

Id is not unique for any of your three entities so you cannot have a many-to-one or one-to-many relationship between them using only an Id property as the principal key (since the principal key must uniquely identify the related record). Id对于您的三个实体中的任何一个都不是唯一的,因此您不能仅使用Id属性作为主键在它们之间建立多对一或一对多关系(因为主键必须唯一标识相关记录) .

Options:选项:

  1. manually join these entities and project the results手动加入这些实体并投影结果
  2. create DB views (if allowed) to provide the junction entities that EF could use for many-to-many relationships.创建数据库视图(如果允许)以提供 EF 可用于多对多关系的联结实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM