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. ListView 删除item删除不了的问题解决办法

    下面的方法是删除不了item的: Integer pos = Integer.valueOf(msg.getBody().toString()); adapter.getList().remove(p ...

  2. 811. Subdomain Visit Count

    这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...

  3. C++中1/0和1/0.0的区别

    参考:https://zhidao.baidu.com/question/1494117716904764979.html 问题说明:在Dev中1/0会报错“除数不得为0”,但是1/0.0不报错,并且 ...

  4. Devops 到底是什么?(转)

    出处:https://www.cnblogs.com/servicehot/p/6510199.html 过去一年以来,一批来自欧美的.不墨守陈规的系统管理员和开发人员一直在谈论一个新概念:DevOp ...

  5. 关于微信小程序中组件和页面对全局样式的继承性

    1.组件只能继承全局样式中的font和color(backgroundcolor不继承) 2.页面可以继承全局样式中所有样式

  6. oracle在exp导出时报错PLS-00201: identifier 'EXFSYS.DBMS_EXPFIL_DEPASEXP' must be declared

    报错如下信息: EXP-00008: ORACLE error 6550 encounteredORA-06550: line 1, column 14:PLS-00201: identifier ' ...

  7. vs2013查看代码性能后删除保存的性能查看文件导致再打开提示未找到与约束匹配的导出

    1.关闭VS: 2.删除C:/Users//AppData/Local/Microsoft/VisualStudio/12.0/ComponentModelCache文件夹下所有文件及文件夹: 3.重 ...

  8. 2-具体学习Github---init add commit log diff

    1.安装: 首先找到git的官网,内部有下载链接. 也可以用下面的,我的是win7的64位系统: 可以在此处下载:Git-2.13.0-64-bit.exe链接:http://pan.baidu.co ...

  9. 锋利的jQuery(第二版)学习总结

    通过对<锋利的jQuery>(第二版)一书的学习,发现此书讲解通俗易懂,是学习jQuery的一本很好的指导书,特作如下总结. 此书主要讲解了jQuery的常用操作,包括认识jQuery,j ...

  10. day2(基础数据类型)

    一.基础数据类型操作 1.数字 int 数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以: int.bit_length() -> int Number of bits necess ...