5.Inheritance Strategy(继承策略)【EFcode-first系列】
我们已经在code-first 约定一文中,已经知道了Code-First为每一个具体的类,创建数据表。
但是你可以自己利用继承设计领域类,面向对象的技术包含“has a”和“is a”的关系即,有什么,是什么,的关系,但是基于SQL关系的实体和数据表集合之前,只是有“has a ”的关系。数据库管理系统(SQL database management systems)不支持继承类型,所以,你怎么将面向对象的领域实体和关系型的数据库映射在一起呢???
下面有三种方式,在Code-First中表现继承:
- Table per Hierarchy (TPH): This approach suggests one table for the entire class inheritance hierarchy. Table includes discriminator column which distinguishes between inheritance classes. This is a default inheritance mapping strategy in Entity Framework.
这种方式,推荐一个表作为整个类的继承层次结构。这个表包含监听列可以和继承类之间很好的区分开来。这个是EF中默认的继承策略。
- Table per Type (TPT): This approach suggests a separate 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 the concrete class.
这个方式,推荐为每一个具体的类,创建一个表,但是抽象类除外。因此如果你在多个具体的类中,继承了抽象类的话,那么这个抽象类的属性将会是每一个具体的类(表)的属性的一部分。
我们在这里不做过多深入的了解,如果想了解更深入的技术,请看:
We are not going into detail here. Visit the following reference link for more detailed information:
- 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)
We have seen default Code First conventions in the previous section. Learn how to configure domain classes in order to override these conventions in the next section.
在之前的环节中,我们已经看到了Code-First默认约定,下面一节,让我们来学一下怎么来配置我们的领域类,为了能够打破默认约定。
PS:这个继承的部分,我后面会深入看下里面的技术,然后翻译出来。请大家多多支持!!!
附上目录:
- 什么是Code First
- 简单的Code First例子
- Code-First 约定
- DB Initialization(数据库初始化)
- Inheritance Strategy(继承策略)
- Configure Domain Classes(配置领域类)
- DataAnnotations(数据注解)
- Fluent API
- Configure One-to-One(配置一对一关系)
- Configure One-to-Many(配置一对多关系)
- Configure Many-to-Many(配置多对多关系)
- Move Configurations(数据迁移)
- DB Initialization Strategy(数据库初始化策略)
- ...待续.....
5.Inheritance Strategy(继承策略)【EFcode-first系列】的更多相关文章
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
原文地址:http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx EF 6 ...
- Hibernate实体对象继承策略
Hibernate继承策略总共同拥有三种,一种是共用一张表:一种是每一个类一张表,表里面储存子类的信息和父类的信息:另一种是通过表连接的方式.每一个类都有一张表,可是子类相应的表仅仅保存自己的信息,父 ...
- EF Code-First 学习之旅 继承策略
Code First中有三种不同的方法表示继承层次关系 1.Table per Hierarchy (TPH): 这种方法建议用一个表来表示整个类的继承层次关系,表中包含一个识别列来区分继承类,在En ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)
一.代码 1. package org.jpwh.model.inheritance.tableperclass; import org.jpwh.model.Constants; import ja ...
- 设计模式之——浅谈strategy模式(策略模式)
strategy模式,即策略模式.个人觉得吧,策略模式更多的是一种思维方式. 首先我们要知道,为什么需要策略模式.举个例子,比如用程序输出今天下午去玩什么. PlayGame 玩游戏 package ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-005Table per subclass with joins(@Inheritance(strategy = InheritanceType.JOINED)、@PrimaryKeyJoinColumn、)
一.结构 The fourth option is to represent inheritance relationships as SQL foreign key associations. Ev ...
- Entity Framework Code-First(7):Inheritance Strategy
Inheritance Strategy in Code-First: We have seen in the Code First Conventions section that it creat ...
- EF CodeFirst系列(3)---EF中的继承策略(暂存)
我们初始化数据库一节已经知道:EF为每一个具体的类生成了数据库的表.现在有了一个问题:我们在设计领域类时经常用到继承,这能让我们的代码更简洁且容易管理,在面向对象中有“has a”和“is a”关系 ...
- Strategy(策略)模式
1.概述 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能.如查找.排序等,一种常用的方法是硬编码(Hard Cod ...
随机推荐
- Hbuilder开发HTML5 APP之图标和启动页制作
1.点击项目下的"manifest.json"文件,会出现自动化的配置工具: 2.点“图标配置“,上传制作好的图标文件,自动生成不同大小的ico,这个要赞下! 3.启动图片(spl ...
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
毕竟人不是神,谁写的程序都会有bug,有了bug不可怕,可怕的是出错了,你却不知道错误在哪里.所以我们需要将应用程序中抛出的所有异常都记录起来,不然出了错,找问题就能要了你的命.下面我们主要讨论的是如 ...
- WPF+通过配置文件生成菜单(Menu)+源码
这个月做项目,遇到过一个通过配置文件来生成菜单的解决方案,感觉挺优雅的,特地放到博客园来,以飨读者. 说来惭愧,以前做的项目都没有这样用过,都是固定死了.如果后续有需要加入菜单,还得在重新修改UI,然 ...
- Leetcode 刷题计划
Two Sum 21.4% Medium Given an array of integers, return indices of the two numbers such that t ...
- JS实战 · 仿css样式选择器
代码如下: <html> <head> <meta http-equiv="Content-Type" content="text/ ...
- Android-简单的图片验证码
Android-图片验证码生成1.为啥要验证码?图片验证码在网络中使用的是比较普遍的.一般都是用来防止恶意破解密码.刷票.论坛灌水.刷页等.2.怎样的验证码比较好?验证码的获取方式无非就两种,一种是后 ...
- Spring声明式事务管理
一.Spring 的声明式事务管理概述 1.Spring 的声明式事务管理在底层是建立在 AOP 的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法 ...
- Jser 设计模式系列之面向对象 - 接口封装与继承
GOF在<设计模式>中说到:面向接口编程,而非面向实现编程 鉴于此,这个概念可见一斑! JS却不像其他面向对象的高级语言(C#,Java,C++等)拥有内建的接口机制,以确定一组对象和另一 ...
- CSS3 Media Queries实现响应式布局
概念我就不在这里写啦.大家可以看看以下网页: http://www.runoob.com/cssref/css3-pr-mediaquery.html http://www.w3cplus.com/c ...
- 从零开始编写自己的C#框架(5)——三层架构介绍
三层架构对于开发人员来说,已经是司空见惯了,除了大型与超小型项目外,大多都是这种架构来进行开发. 在这里为初学者们简单介绍一下三层架构: (下面内容摘自<趣味理解:三层架构与养猪—<.NE ...