简体   繁体   English

实体框架-如何在类层次结构中避免TPC

[英]Entity Framework - How to avoid TPC in a class hierarchy

In my business domain I have two entities which, as objects, defines a hierarchical relationship according to the properties they expose but that at the Data Base level they are very different. 在我的业务领域中,我有两个实体,它们作为对象根据它们公开的属性定义层次结构关系,但是在数据库级别上,它们是非常不同的。

Specifically, what I have is an Image class, which defines the properties A and B (beside the Id), and several simple methods. 具体来说,我所拥有的是一个Image类,它定义了属性A和B(在ID旁边)以及一些简单的方法。 Then I have a Thumbnail class, which is exactly the same than Image. 然后,我有一个Thumbnail类,它与Image完全相同。

Inside an OOP perspective, it is logical to make Thumbnail inherit from Image. 在OOP透视图中,使Thumbnail从Image继承是合乎逻辑的。 But, at the Db level these two entities differ in one important detail: Image declare a FK to another table and Thumbnail not. 但是,在Db级别上,这两个实体在一个重要的细节上有所不同:Image向另一个表声明FK,而Thumbnail没有。

Actually, Image defines the set of image that a Product (for example) can have (Many-to-One) but Thumbnail defines the only thumbnail that the same Product can have (One-or-Zero-to-One). 实际上,“图像”定义了一个产品(例如)可以具有的图像集(“一对多”),而“缩略图”定义了同一产品可以具有的“唯一图像”(“一对零”)。 In this case, the Thumbnail wouldn't be in the Image set. 在这种情况下,缩略图将不在图像集中。

So, at the DB, the Image table should have columns A, B, Id and the FK to Product while the Thumbnail table should only have columns A, B and Id (that also will be the FK to Product). 因此,在数据库中,“图像”表应具有A,B,Id和FK到产品的列,而“缩略图”表应仅具有A,B和Id列(也将是到产品的FK)。

If I model this using EF Code First, at best (as I could) it generates a table for Image and then one for Thumbnail and a One-or-Zero-to-One association between Image and Thumbnail. 如果我首先使用EF Code对此模型进行建模,那么(最好)将为Image生成一个表,然后为Thumb表生成一个表,并在Image和Thumbnail之间建立一对一或零对一的关联。 This association is the one that I'm trying to avoid, because for adding a Thumbnail I have to also add it as an image and then setting the FK, which is impossible because it have none. 这种关联是我要避免的一种关联,因为添加缩略图时,我还必须将其添加为图像,然后设置FK,这是不可能的,因为没有缩略图。

If I explicitly specify to generate a TPC then it doesn't allows me to establish an association between Product an Image because associations shall be defined only in the most derived types. 如果我明确指定生成TPC,则它不允许我在“产品与图像”之间建立关联,因为关联只能在大多数派生类型中定义。

Do you have any ideas? 你有什么想法?

You need to configure your entities, so that they use Mapping the Table-Per-Concrete Class (TPC) Inheritance : 您需要配置您的实体,以便它们使用Map-Table-Per-Concrete类(TPC)继承

In the TPC mapping scenario, all non-abstract types in the hierarchy are mapped to individual tables. 在TPC映射方案中,层次结构中的所有非抽象类型都映射到各个表。 The tables that map to the derived classes have no relationship to the table that maps to the base class in the database. 映射到派生类的表与映射到数据库中基类的表没有关系。 All properties of a class, including inherited properties, are mapped to columns of the corresponding table. 类的所有属性(包括继承的属性)都映射到相应表的列。

This is a sample of a possible configuration using TPC:: 这是使用TPC的可能配置的示例::

modelBuilder.Entity<Image>() 
  .Property(c => c.ImageID) 
  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

modelBuilder.Entity<ThumbNail>().Map(m => 
{ 
    m.MapInheritedProperties(); 
    m.ToTable("Thumbnails");
}); 

You'll have to fine tune it for your particular purposes. 您必须针对特定目的对其进行微调。 For example, excluding the FK property with: 例如,使用以下命令排除FK属性:

modelBuilder.Entity<Thumbnail>().Ignore(p => p.FkProperty);

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

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