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:

  1. Create a project named TPTTest

  2. 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; }
    }
  3. 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; }
    }
  4. Execute Command Enable-Migrations in nuget command line

  5. Exceute Command Add-Migration Init

  6. Exceute Command Update-Database

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

  1. Lerning Entity Framework 6 ------ Introduction to TPH

    Sometimes, you have created two models. They have the same parent class like this: public class Pers ...

  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. 2017/2/11CSS基础

    一:html中div: 1.DIV标签应用于 Style Sheet(样式表)方面会更显威力,它最终目的是给设计者另一种组织能力,有 Class.Style.title.ID 等属性. 2.<d ...

  2. 使用kbmmw smart service 属性时的一个注意事项

    kbmmw 5.0 以后支持smart service, 这个用起来非常方便,kbmmw 通过 定制属性来简化编程,可以参考我以前的文章.但是这个意味着使用单元引用一定要小心, 否则出了问题,都不知道 ...

  3. 2018.11.17 codechef PRIMEDST(点分治+fft)

    传送门 fftfftfft一眼题(其实最先想到的是nttnttntt,wawawa了几次之后发现模数不够大果断弃疗写fftfftfft) 我们点分治统计答案的个数. 考虑现在已经统计出了到当前点的所有 ...

  4. mysql 外键理解

    假定一个班级的学生个人信息表: 什么是外键 在设计的时候,就给表1加入一个外键,这个外键就是表2中的学号字段,那么这样表1就是主表,表2就是子表.(注意: 外键不一定须要作为从表的主键.外键也不一定是 ...

  5. vue中使用promise.all发送多个请求

    1.创建两个promise,在promise中使用axios 2.调用Promise.all([p1,p2]).then(res=>{}).catch(err=>{})方法 代码如下: & ...

  6. Matlab编辑器背景修改

    将下段代码如到C:\Users\Peng Chen\AppData\Roaming\MathWorks\MATLAB\R2016a\matlab.prf 先备份.prf,再替代之前的. #MATLAB ...

  7. 使用Docker镜像

    1     使用Docker镜像 1.1   获取镜像 命令格式:docker pull NAME[:TAG] NAME为镜像仓库的名称 TAG为镜像的标签(表示版本号) 描述一个镜像需要包括:名称+ ...

  8. s4-4 以太网概述

    以太网所处的位置 以太网的发展史 1973 Robert Metcalfe及其同事设计了以太网雏形(施乐公司) 1980 DIX发布最早的以太网标准,开放标准 1985 IEEE802.3对以太网作了 ...

  9. es6 字符串方法

    1.字符串的新方法  includes() 包含属性 startsWith() 头部开始是否包含 endWith() 字符串是否在尾部   ========三个返回值都为布尔值  第二参数为数字  e ...

  10. 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...