Lerning Entity Framework 6 ------ Introduction to TPH
Sometimes, you have created two models. They have the same parent class like this:
public class Person
{
public int PersonId { get; set; }
public string PersonName { get; set; }
}
public class InsidePerson : Person
{
public string Title { get; set; }
public string Department { get; set; }
}
public class OutsidePerson : Person
{
public string CompanyName { get; set; }
}
After you execute the command Update-Database in nuget command line, Entity Framework will create one table named people:

The Discriminator column is created for discriminating what model dose current row represent. this model creating type is called TPH(Table per Hierarchy Inheritance). Let's do a test. Add some codes in main function:
static void Main(string[] args)
{
using (MyDbContext db = new MyDbContext())
{
Person insidePerson1 = new InsidePerson()
{
PersonName = "InsidePerson1",
Title = "Manager",
Department = "development"
};
db.People.Add(insidePerson1);
InsidePerson insidePerson2 = new InsidePerson()
{
PersonName = "InsidePerson2",
Title = "Manager",
Department = "development"
};
db.People.Add(insidePerson2);
InsidePerson insidePerson3 = new InsidePerson()
{
PersonName = "InsidePerson3",
Title = "Manager",
Department = "development"
};
db.InsidePeople.Add(insidePerson3);
Person outsidePerson1 = new OutsidePerson()
{
PersonName = "outsidePerson1",
CompanyName = "Tencent"
};
db.People.Add(outsidePerson1);
db.SaveChanges();
}
}
Let's look at the database:

If you don't like the discriminator column which entity framework auto create, you can define your column by adding these codes in OnModelCreating function of DbContext class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Person>()
.Map<InsidePerson>(p => p.Requires("PersonType").HasValue(1))
.Map<OutsidePerson>(p => p.Requires("PersonType").HasValue(2));
}
Then, execute the command Add-Migration AddPesonTypeColumn2PeopleTable and Update-Database in nuget command line. Now, look at the database again:

We can find the Entity Framework can't insert any value into PersonType column of existed rows. It's a little sad. Now, We insert some new data by coding:
static void Main(string[] args)
{
using (MyDbContext db = new MyDbContext())
{
Person insidePerson4 = new InsidePerson()
{
PersonName = "InsidePerson4",
Title = "Manager",
Department = "development"
};
db.People.Add(insidePerson4);
Person outsidePerson2 = new OutsidePerson()
{
PersonName = "outsidePerson2",
CompanyName = "Baidu"
};
db.People.Add(outsidePerson2);
db.SaveChanges();
}
}
Look at the databas again:

That's all.
Lerning Entity Framework 6 ------ Introduction to TPH的更多相关文章
- Lerning Entity Framework 6 ------ Introduction to TPT
Sometimes, you've created a table for example named Person. Just then, you want to add some extra in ...
- Lerning Entity Framework 6 ------ Defining Relationships
There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...
- Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database
The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...
- Lerning Entity Framework 6 ------ Working with in-memory data
Sometimes, you need to find some data in an existing context instead of the database. By befault, En ...
- Lerning Entity Framework 6 ------ Inserting, Querying, Updating, and Deleting Data
Creating Entities First of all, Let's create some entities to have a test. Create a project Add foll ...
- Lerning Entity Framework 6 ------ Defining the Database Structure
There are three ways to define the database structure by Entity Framework API. They are: Attributes ...
- Lerning Entity Framework 6 ------ Complex types
Complex types are classes that map to a subset of columns of a table.They don't contains key. They a ...
- Lerning Entity Framework 6 ------ Using a commandInterceptor
Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...
- Lerning Entity Framework 6 ------ A demo of using Entity framework with MySql
Create a new project named MySqlTest Install following packages by right-clicking on the References ...
随机推荐
- JUC知识点总结图
转载http://www.jsondream.com/2017/06/12/about-JUC.html
- hdu--6178(多校
题意:要在一棵 n 个点的树上放 k 只猴子,然后删掉尽量多的边,使得删边后,每只猴子都至少和另外一只猴子相连,问最后剩下的边数. 思路:其实dfs遍历一次看有多少个点-边-点就好了,比赛的时候就觉得 ...
- linux之网络
一 什么是网络,网络能干什么 网络出现的主要目的就是实现主机和主机之间的通信,而互联网协议(Internet Protocol Suite)就是连接两台计算机之间的internet一系列统一的标准.互 ...
- 百度api接口_知识积累
app_id, app_key, app_secret app_id 是用来标记你的开发者账号的, 是你的用户id, 这个id 在数据库添加检索, 方便快速查找 app_key 和 app_secre ...
- DDR4 PSOD输出的优点--DBI的优点
DDR4是JEDEC组织关于DRAM器件的下一代标准.DDR4主要是针对需要高带宽低功耗的场合.这些需求导致了DDR4芯片引入了一些新的特点,这些新的特点,导致在系统设计中,引入一些新的设计需求. D ...
- 诡异的 ERROR 1045 (28000): Access denied for user 错误
问题描述: 用户已建,权限已赋予.long long ago这个用户是可以正常访问的,但是今天它就不能访问了.报错如下: ERROR 1045 (28000): Access denied for u ...
- redis概览
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括string(字符串 ...
- GDAL读写矢量文件——Python
在Python中使用OGR时,先要导入OGR库,如果需要对中文的支持,还需要导入GDAL库,具体代码如下.Python创建的shp结果如图1所示. 图1 Python创建矢量结果 #-*- codin ...
- xslt 和一个demo
https://www.w3.org/1999/XSL/Transform Specifications The XSLT language has three versions which are ...
- SecureCRTv7.3 和 navicat110_mysql
激活步骤: 一.首次使用: 1.保持SecureCRT未打开. 2.打开注册机keygen.exe文件(Windows vista ,7,8需要以管理员身份运行),点击[Patch]按钮,会让你选择文 ...