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 ...
随机推荐
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- 别人的Linux私房菜(2)Linux简介
同一操作系统无法在不同硬件平台上运行.架构. Bell实验室和麻省理工学院MIT和通用电气公司GE发起了Multics计划,分时兼容系统,300以上多终端连接主机. Unics 由Multics中的人 ...
- python中将两个数组压缩成一个数组
我们有时候会遇到一个问题将两个数组一一对应的压缩起来: 两个都是字符串: 列表解析[''.join(i) for i in zip(list_1, list_2)] map(lambda x,y:x+ ...
- linux学习--查看cpu及内存信息
查看物理cpu个数: cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l 查看每个cpu核数 cat /proc/cp ...
- C++STL list
list双向链表 高效进行插入删除数据 不可以随机存取元素,所以不支持at()和[]操作符.it可以++ --,不能it+5 节点序号从0开始 list<int> l; l.push_b ...
- git——^和~的区别(转)
原文地址: http://www.cnblogs.com/softidea/p/4967607.html 一. 引子 在git操作中,我们可以使用checkout命令检出某个状态下文件,也可以使用re ...
- python3.4用函数操作mysql5.7数据库
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...
- BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 4493 Solved: 2695[Submit][Statu ...
- webService之helloword(java)rs
webservice之rs(helloworld) 1.pom.xml文件 <dependencies> <!-- 使用CXF RS开发 --> <dependency& ...
- redis状态监控可视化工具RedisLive使用
首先,别人写的工具,赞一下 github地址 https://github.com/nkrode/RedisLive 然后,fork一下,自己加点功能 gui介绍(直接copy的github图片) ...