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. 【搜索】Shuffle'm Up

    运用第i个s12和第i+1个s12中,每个位置具有的确定的映射关系: pos = pos * 2 + 1 (pos < c) pos = pos * 2 - c * 2 (pos >= c ...

  2. TP5在lnmp环境中不能重写的问题

    说到坑,这个问题困扰了我一两天时间,本地可以,线上环境检查了好久. 基本检查的地方有几个了,首先就是nginx下面的重写配置,这个大家在网上都能搜到,至于定义的变量和配置路径,修改一下即可. 还有就是 ...

  3. js中定时器相关

    每三秒(3000 毫秒)弹出 "Hello" : setInterval(function(){ alert("Hello"); }, 3000); setIn ...

  4. warning: this decimal constant is unsigned only in ISO C90问题的处理及理解

    参考:https://blog.csdn.net/duguduchong/article/details/7709482 https://bbs.csdn.net/topics/391892978?p ...

  5. java sigar.jar

    http://blog.csdn.net/yin_jw/article/details/40151547 DEBUG Sigar - no libsigar-x86-linux.so in java. ...

  6. 关于SQL表字段值缺失的处理办法

    在计算收益率时候,  收益率 = 收益 / 成本 一.如果成本为0,NULL,此时无法计算收益率: 方法: 1.将成本为0的数据 运算 (case when  cost =0 or cost is n ...

  7. CodeForces 916B Jamie and Binary Sequence (changed after round) (贪心)

    题意:给定两个数字n,m,让你把数字 n 拆成一个长度为 m 的序列a1,a2,a3...am,并且∑2^ai = n,如果有多组,要求序列中最大的数最小,然后再相同就要求除了最大数字典序最大. 析: ...

  8. 如何制作chm文件

    本文介绍如何从一个包中的docs文档生成一个chm文档. 1,准备软件Easy CHM 这个网上有下载,下载后安装,至于怎么使用,等下再介绍.安装之后如下图. 2,准备文件 比如我这里下载了一个cxf ...

  9. T-SQL查询的逻辑处理过程

    对于一个查询SQL而言,通常将其逻辑处理过程分成7个大的阶段 分别是: 1.FROM 2.WHERE 3.GROUP BY 4.HAVING 5.SELECT 6.ORDER BY 7.TOP/OFF ...

  10. VIP之FrameBuffer

     2.VIP Frame Buffer 1.原来我是一直存在一个疑惑,demo上说VIP Frame Buffer输出是固定的60fps,但是在NiosII的程序中我没有找到设置输出为60fps的设置 ...