EF继承关系映射
继承映射策略的三种策略
There are following three different approaches to represent an inheritance hierarchy in Code First:
- Table per Hierarchy (TPH): This approach suggests one table for entire class inheritance hierarchy. Table includes discriminator column which distinguish between inheritance classes. This is a default inheritance mapping strategy in Entity Framework.
- Table per Type (TPT): This approach suggests seperate table for each domain class.
- Table per Concrete class (TPC): This approach suggests one table for one concrete class, but not for the abstract class. So if you inherit the abstract class in multiple concrete classes then the properties of the abstract class will be part of each table of concrete class.
Code First 有以下三种不同的方法来表示继承层次结构:
- 每个层次结构一张表 (TPH): 这种方法表明,为整个类继承层次结构一个表。表包括鉴别器列(discriminator )的区分继承类。这是在实体框架中的默认继承映射策略。
- 每个类型一张表 (TPT): 这种方法显示单独的表中为每个域类。
- 每个具体类 一张表(TPC) : 这种方法显示一个表为一个具体的类,而不是抽象类。因此如果你继承的抽象类在多个具体的类然后抽象类的属性中将每个表具体类的一部分。


----------------------------------------------------------------------------


---------------------------------------------------


三种策略的适用场景
I want to emphasize that there is no one single "best strategy fits all scenarios" exists. As you saw, each of the approaches have their own advantages and drawbacks. Here are some rules of thumb to identify the best strategy in a particular scenario:
- If you don’t require polymorphic associations or queries, lean toward TPC—in other words, if you never or rarely query for BillingDetails and you have no class that has an association to BillingDetail base class. I recommend TPC (only) for the top level of your class hierarchy, where polymorphism isn’t usually required, and when modification of the base class in the future is unlikely.
- If you do require polymorphic associations or queries, and subclasses declare relatively few properties (particularly if the main difference between subclasses is in their behavior), lean toward TPH. Your goal is to minimize the number of nullable columns and to convince yourself (and your DBA) that a denormalized schema won’t create problems in the long run.
- If you do require polymorphic associations or queries, and subclasses declare many properties (subclasses differ mainly by the data they hold), lean toward TPT. Or, depending on the width and depth of your inheritance hierarchy and the possible cost of joins versus unions, use TPC.
By default, choose TPH only for simple problems. For more complex cases (or when you’re overruled by a data modeler insisting on the importance of nullability constraints and normalization), you should consider the TPT strategy. But at that point, ask yourself whether it may not be better to remodel inheritance as delegation in the object model (delegation is a way of making composition as powerful for reuse as inheritance). Complex inheritance is often best avoided for all sorts of reasons unrelated to persistence or ORM. EF acts as a buffer between the domain and relational models, but that doesn’t mean you can ignore persistence concerns when designing your classes.
我想强调的是没有一单"最佳策略适合所有场景"存在。正如你所看到的每个方法有自己的优点和缺点。以下是一些经验法则来确定在特定的情况下最好的策略:
- 如果你不需要多态关联或查询,倾向于 TPC — — 换句话说,如果你从来没有或很少查询 BillingDetails 和你没有类关联 BillingDetail 基类。我推荐 TPC (仅限于) 您的类层次结构的顶层多态性并不是经常需要的而且将来也不太可能修改基类。
- 如果您需要多态关联或查询,并子类相对定义了几个属性 (特别是如果子类之间的主要区别是在他们的行为),倾向于 TPH。你的目标是尽量减少的可以为 null 的列数,并说服自己 (和您的 DBA) 非规范化的架构在长期内不会产生问题。
- 如果您需要使用多态关联或查询,并且子类(主要由它们拥有的数据不同的子类)定义了很多属性,倾向于 TPT。或者,如果考虑到继承层次结构的宽度和深度和Joins与Unions的可能产生的成本,使用 TPC。
默认情况下,选择 TPH 仅为简单的问题。对于更复杂的情况下 (或当你正在推翻由数据建模者坚持的为空性约束和归一化的重要性),你应该考虑 TPT 战略。但在这一点上,问一问自己,是否它可以用delegation 修改下对象模型中的继承 (代表团是作文一样强大的复用作为继承的一种方式)。复杂的继承通常最好能够通过许多与持久化或 ORM 无关的原因避免。虽然EF 充当域和关系模型之间的缓冲,但这并不意味着设计您的类时,您可以忽略关注持久化的内容。
参考
- Inheritance with EF Code First: Table per Hierarchy (TPH)
- Inheritance with EF Code First: Table per Type (TPT)
- Inheritance with EF Code First: Table per Concrete class (TPC)
EF继承关系映射的更多相关文章
- 在Entity Framework 中实现继承关系映射到数据库表
继承关系映射到数据库表中有多种方式: 第一种:TPH(table-per-hiaerachy) 每一层次一张表 (只有一张表) 仅使用名为父类的类型名的一张表,它包含了各个子类的所有属性信息,使用区分 ...
- EF中关系映射问题
一对一,和一对多的简单问题就部说了,直接来多对多这样的问题吧. 首现关系映射为这样的: /// <summary> /// 对应数据库中dbo.Address表 /// </summ ...
- entityframework学习笔记--007-实体数据建模基础之继承关系映射TPT
Table per Type Inheritance (TPT)建模 1.假设你有两张表与一张公共的表密切相关,如图7-1所示,Businiss表与eCommerce表.Retail表有1:0...1 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (8) -----第二章 实体数据建模基础之继承关系映射TPT
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 2-8 Table per Type Inheritance 建模 问题 你有这样一 ...
- entityframework学习笔记--008-实体数据建模基础之继承关系映射TPH
Table per Hierarchy Inheritance 建模 1.让我们假设你有如图8-1中的表,Employee表包含hourly employees 和salaried employees ...
- 《Entity Framework 6 Recipes》中文翻译系列 (9) -----第二章 实体数据建模基础之继承关系映射TPH
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 2-10 Table per Hierarchy Inheritance 建模 问题 ...
- Hibernate之实体关系映射
延迟加载与即时加载 例如Person类和Email类是一对多关系,如果设为即时加载,当加载Person时,会自动加载Email,如果设置为延迟加载,当第一次调用person.getEmails()时才 ...
- C# 数据操作系列 - 6 EF Core 配置映射关系
0. 前言 在<C# 数据操作系列 - 5. EF Core 入门>篇中,我们简单的通过两个类演示了一下EF增删改查等功能.细心的小伙伴可能看了生成的DDL SQL 语句,在里面发现了些端 ...
- hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系
hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持 ...
随机推荐
- Visual Studio 2005 搭建Windows CE 6.0环境之准备
Microsoft Visual Studio 2005 Visual Studio 2005 Professional 官方90天试用版英文版:http://download.microsoft.c ...
- jQuery美女幻灯相册轮播源代码
体验效果:http://hovertree.com/texiao/jquery/ 本幻灯片包含小图列表和大图轮播,包含图片标题和详细介绍,详细介绍字数可以很多,每张图片包含链接,可以实现跳转 HTML ...
- 积累一下SQL
开篇先自我检讨一下,写了博客几年以来首次试过连续两个月没出过博文,有客观也有主观原因,但是最近这年里博文数量也越来越少,博文的质量也每况日下.希望自己一直能坚持下来,多写写博文,这月尽量多写几篇来弥补 ...
- Java的HTTP通信
在Android中,HTTP通信可以用Volley,在Java中不能使用Volley,只能使用DefaultHttpClient,HttpPost和HttpResponse. /* * 向服务器发送数 ...
- css(二)
本文是一些作者在长期写代码中总结的常用css查询,写在本博客中,方便以后查询. 1. 颜色属性: color HEX(十六进制色:color: #FFFF00 --> 缩写:#FF0) RG ...
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(九)地图定位模块
config.xml文件的配置如下: <widget label="地图定位" config="widgets/esri/Location/LocationWidg ...
- C# Stream 和 byte[] 之间的转换
一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...
- ArcGIS 10.5新功能预览
ArcGIS for Server产品线被重命名为ArcGIS Enterprise. 带来更多丰富的时空GIS功能. 分析地理大数据 捕捉和分析实时传感器数据 快速地理影像分析 ArcGIS Ent ...
- 深入浅出React Native 3: 从零开始写一个Hello World
这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...
- 在 CentOS7 上安装 Tomcat9
在 CentOS7 上安装 Tomcat9 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中: cd /usr/local/ 3 创建目 ...