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. js中 xpath 使用

    一.使用: 非IE浏览器,使用 document.evaluate var result = document.evaluate("//a[@href]", document, n ...

  2. 初识jvm堆,栈参数

    堆的分配参数: -Xmx //设立最大堆 -Xms //最小堆,初始化堆大小 -Xmn  //设置新生代(eden+2*surviivor+old)大小   官方推荐:3/8Xmx------> ...

  3. mysql 外键理解

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

  4. Basic Router Architecture

    from the book principles and practices of interconnection networks  the chapter router architecture ...

  5. 学以致用三----centos7.2基本环境补充

    补充: 在上一篇里,加时间戳 echo ‘export HISTTIMEFORMAT ="%F %T `whoami`" ’ >> /etc/profile sourc ...

  6. Effective C++ 随笔(3)

    条款12: 以对象管理资源 两种只能指针: std:auto_ptr<> 当使用copy操作室,原先的智能指针指向为null std:tr1:share_ptr<int> sp ...

  7. Effective C++ 随笔(2)

    条款5 了解c++默默编写并调用哪些函数 编译器自动生成的copy 构造函数,copy赋值操作符,析构函数,构造函数,这些都是public和inline的,此处inline的意思是他们的定义都是在头文 ...

  8. PHP发起POST DELETE GET POST 请求

    原文链接:http://blog.csdn.net/lengxue789/article/details/8254667 关于POST,DELETE,GET,POST请求 get:是用来取得数据.其要 ...

  9. Ubuntu 12.10 安装VirtualBox增强功能

    原文链接:http://fengbaoxp.iteye.com/blog/1871825 Ubuntu 12.10 Desktop         首先,通过VirtualBox菜单(设备->安 ...

  10. 1111 Online Map (30 分)

    1111. Online Map (30)Input our current position and a destination, an online map can recommend sever ...