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 information, but don't want to add any new field into the existed table Person. You just want to add a new table named PersonContact. Both two table have the same primary key, it's a kind of one to one or one to zero relationship. There's a model crating type named TPT(Table per Type Inheritance). let's do it now:
Create a project named TPTTest
Create some models like these
public class Person
{
public int PersonId { get; set; } public string PersonName { get; set; }
} [Table("PersonContact")]
public class PersonContact: Person
{
public string Address { get; set; } public string Email { get; set; } public string Mobile { get; set; }
} [Table("PersonPhysiclalStatus")]
public class PersonPhysiclalStatus:Person
{
public int Weight { get; set; } public int Height { get; set; }
}
Create the DbConext
class MyContext:DbContext
{
public MyContext():base("name=Test")
{ } public DbSet<Person> Persons { get; set; } public DbSet<PersonContact> PersonContacts { get; set; } public DbSet<PersonPhysiclalStatus> PersonPhysiclalStatus { get; set; }
}
Execute Command Enable-Migrations in nuget command line
Exceute Command Add-Migration Init
Exceute Command Update-Database
Look at the database:

The most Import step is Adding the Table attribute to the PersonContact and PersonPhysiclalStatus class. If you passed this step, after execute the Command Update-Database, there's only one table named people that contains all fields.
Lerning Entity Framework 6 ------ Introduction to TPT的更多相关文章
- Lerning Entity Framework 6 ------ Introduction to TPH
Sometimes, you have created two models. They have the same parent class like this: public class Pers ...
- 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 ...
随机推荐
- Android Camera后台拍照
http://item.congci.com/item/android-camera-houtai-paizhao 有许多人希望在不让用户知道的情况下,使用Android后台Service调用摄像头拍 ...
- [RequireComponent(typeof(....))]
RequireComponent的使用: 当你添加的一个用了RequireComponent组件的脚本,需要的组件将会自动被添加到game object(游戏物体).这个可以有效的避免组装错误.举个例 ...
- c++四舍五入的新方法
将原来的数加上0.5,如果是需要进位的加上0.5就进位了,如果不需要进位的加上0.5也小于1,被int型省略掉.
- 【转】python 2.6.6升级到python 2.7.x版本的方法
1.下载python2.7.x wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz 2.解压并编译安装 tar -zxvf Py ...
- 2019.02.06 bzoj4503: 两个串(fft)
传送门 题意简述:给两个字符串s,ts,ts,t,ttt中可能有通配符,问ttt在sss出现的次数和所有位置. 思路:一道很熟悉的题,跟bzoj4259bzoj4259bzoj4259差不多的. 然后 ...
- str.index()与str.find()比较
def extract_from_tag(tag,line): opener = "<" + tag + ">" closer = "&l ...
- 第二章(java程序设计)第三章(语言基础)
第二章 2.1 对象 对象的概念是由现实世界引入问题模型: 对象包含有:状态和行为.具体地来说是: 数据封装:对象的方法的作用就是:将内部变量封装起来,提供给外界交互的窗口.(实现对数据的隐藏) 继承 ...
- 如何比较两个xml 的异同
http://www.xmlunit.org/ <dependency> <groupId>org.xmlunit</groupId> <ar ...
- Ng第十四课:降维(Dimensionality Reduction)
14.1 动机一:数据压缩 14.2 动机二:数据可视化 14.3 主成分分析问题 14.4 主成分分析算法 14.5 选择主成分的数量 14.6 重建的压缩表示 14.7 主成分分析法 ...
- Eclipse添加servlet-api.jar库的引用
右键Application-->Properties-->Java Build Path-->Libraries-->Add External JARs-->servle ...