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: 设定待映射的持 ...
随机推荐
- CocosStudio文件解析工具CsdAnalysis
起因 因为工作需要,所以需要使用CocosStudio来制作界面动画什么的.做完了发现需要找里边对象的时候会有很长一串代码,感觉不是很爽.之前写OC代码的时候可以吧程序中的对象指针跟编辑器中的对象相对 ...
- 比较牛X的互联网公司都有哪些作死的行为
以下为近乎家的小近吐血整理: 1流氓行为 臭表碾说的就是你们! 百度 还有这种伪造网页弹窗: 360 不经同意,也不弹窗提醒,直接给我们安装推广软件.比较典型的是 腾讯 腾讯一直走在行业最前端,买 ...
- C#中实现excel文件批量导入access数据表中
一 .界面简单设计如下: 二 .代码如下: using System; using System.Collections.Generic; using System.ComponentModel; u ...
- IOS学习笔记之获取Plist文件读取数据
@property(nonatomic,strong) NSArray *pic; //创建数组属性 @property(nonatomic,assign) int index; //创建索引属性 @ ...
- 为什么document.firstChild找到的不是html节点
DOM是针对HTML4.01开发的,我们现在是XHTML1.0. 所以要想使用核心DOM中的属性和方法,必须去掉DTD类型定义. <!DOCTYPE html PUBLIC "-//W ...
- win10平台mysql5.6.34免安装版(绿色版zip)的配置以及密码和编码设置
平台:win10 X64 mysql: mysql-5.6.34-winx64.zip 以下所写都是本人测试过的,力争无误.上次发布了,可是发现了一些问题,特地查了一下官方文档(我会说我是用有道词典翻 ...
- php实现设计模式之 访问者模式
<?php /** * 访问者模式 * 封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作. * 行为类模式 */ /** 抽象访问者:抽象类或 ...
- MongoDb gridfs-ngnix文件存储方案
在各类系统应用服务端开发中,我们经常会遇到文件存储的问题. 常见的磁盘文件系统,DBMS传统文件流存储.今天我们看一下基于NoSQL数据库MongoDb的存储方案.笔者环境 以CentOS ...
- 深入理解Javascript中构造函数和原型对象的区别
在 Javascript中prototype属性的详解 这篇文章中,详细介绍了构造函数的缺点以及原型(prototype),原型链(prototype chain),构造函数(constructor) ...
- xp系统下硬盘安装centos6.5
引言: 电脑系统是Windows XP,电脑没有光驱.手头没有U盘.没有移动硬盘.电脑主板不支持U盘启动,在这种情况下想安装CentOS 6.0,有木有办法? 答案:有办法,请看下面教程! 必备工具: ...