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 ...
随机推荐
- Linux入门练习操作命令
查看目录命令 1. 显示目录下所有文件 2.显示所有文件,包括隐藏文件 创建目录命令 1.在改目录下创建文件夹“practise” 切换目录 1.切换到指定的目录 2.切换到上一级目录 3.还在当前目 ...
- maven-javadoc-plugin 出现错误Unsupported major.minor version 51.0
[INFO] --- maven-javadoc-plugin:3.0.0:jar (attach-javadocs) @ eii-frame-sms ---[WARNING] Error injec ...
- Python Tutor
Python Tutor Python Tutor 是由 Philip Guo 开发的一个免费教育工具,可帮助学生攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程.通过这个工具, ...
- Spark学习笔记-GraphX-1
Spark学习笔记-GraphX-1 标签: SparkGraphGraphX图计算 2014-09-29 13:04 2339人阅读 评论(0) 收藏 举报 分类: Spark(8) 版权声明: ...
- PHP字符串替换函数
str_replace函数 描述:实现字符串替换,区分大小写 语法:mixed str_replace(mixed $search, mixed replace, mixed $subject, [i ...
- python学习 day20 (3月27日)----(单继承多继承c3算法)
继承: 提高代码的重用性,减少了代码的冗余 这两个写法是一样的 Wa('青蛙').walk() #青蛙 can walk wa = Wa('青蛙') wa.walk() #青蛙 can walk 1. ...
- 27 isinstance与issubclass、反射、内置方法
isinstance与issubclass issubclass:判断子类是否属于父类,是则返回True,否则返回False isinstance:判断对象是否属于类,是则返回True,否则返回Fal ...
- 算法工程师B
美团点评2017校招笔试真题-算法工程师B 1.以下关于经典的k-means聚类的说法哪个是错误的? A:k-means聚类算法是全局收敛的 B:k-means的聚类结果和初始聚类中心点的选取有关 ...
- js 获取数组最后一个元素
当然有很多中做法 我这边就随便写几个最常用 最简单的方法把 # shift 删除数组第一个元素,并返回该元素,跟pop差不多 var a = ["aa","bb" ...
- OpenCV(2):视频
播放AVI视频 #include<iostream> #include<opencv2/core/core.hpp> #include<opencv2/highgui/h ...