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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Lerning Entity Framework 6 ------ Defining the Database Structure

    There are three ways to define the database structure by Entity Framework API. They are: Attributes ...

  7. 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 ...

  8. Lerning Entity Framework 6 ------ Using a commandInterceptor

    Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...

  9. 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 ...

随机推荐

  1. C语言基础第五次作业

    题目7-2 统计一行文本的单词个数 1.实验代码 #include <stdio.h> int main() { char a; ,countword=; ){ scanf("% ...

  2. day13作业—(登录注册)

    2.写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用户名密码通过|隔开,每个人的用户名密码占用文件中一行. 完成三次验证, ...

  3. python console 设立快捷键 学习源码 用到英语

    arbitrary---随意 iterable----迭代 invalid syntax   -----无效的语法 subscriptable   ----可索引访问的

  4. 836. Rectangle Overlap

    class Solution { public: bool isRectangleOverlap(vector<int>& rec1, vector<int>& ...

  5. 爬取数据时解析url时一直报错Caused by: java.net.URISyntaxException: Illegal character in query at index 823替换了所有空格和特殊字符还是无效

    近日在用HttpClient访问抓取汇率时,为了省力,直接采用 String url = "http://api.liqwei.com/currency/?exchange=usd|cny& ...

  6. 2019.01.23 hdu3377 Plan(轮廓线dp)

    传送门 题意简述:给一个n*m的带权矩阵,求从左上角走到右下角的最大分数,每个格子只能经过最多一次,n,m≤9n,m\le9n,m≤9. 思路: 考虑轮廓线dpdpdp,但这道题并没有出现回路的限制因 ...

  7. mac os下 android studio真机调试

    http://www.cnblogs.com/developer-wang/p/6719555.html 如果没有 .bash_profile 只需要创建 .bash_profile,然后增加andr ...

  8. idea中Eclipse Code Formatter插件设置和使用,以及注释模板的修改

    在settings里面找到plugins这个选项,搜索Eclipse Code Formatter,点击安装,重启idea即可进行配置: 首先,先安装Eclipse Code Formatter插件: ...

  9. windows访问ubuntu的文件

    前提:windows电脑和ubuntu电脑要工作在同一个网段! 1.先要安装Samba sudo apt-get install samba openssh-server 2.编译Samba配置文件 ...

  10. DDR中的命令

    (3) 列地址选择(CAS#): 选择器件内感兴趣的地址列 行地址选择(RAS#): 选择设备中感兴趣的地址行. (4)(precharge)预充电 DRAM读取具有破坏性,也就是说,在读操作中会破坏 ...