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. 好像leeceode题目我的博客太长了,需要重新建立一个. leecode刷题第二个

    376. Wiggle Subsequence               自己没想出来,看了别人的分析. 主要是要分析出升序降序只跟临近的2个决定.虽然直觉上不是这样. 455. 分发饼干     ...

  2. redis 和 kookeeper 连用 构建 redis集群

    转载地址:https://www.zhihu.com/question/62598701

  3. 解决mysql默认的8小时自动断开连接

    语言:javaEE 框架:spring mvc+spring+mybatis 数据库:mysql8 WEB服务器:tomcat8 背景: 在试运营阶段发现发生“连接超时”异常 抛出异常: Cause: ...

  4. timerfd与eventfd

    1.timerfd timerfd是定时器描述符,通过timerfd_create()来创建它,timerfd_settime()来设置定时器时间,当时间到期定时器文件描述符就可读,所以能够在sele ...

  5. 轮询、中断、DMA和通道

    from http://blog.csdn.net/lastsweetop/article/details/3418769 一.轮询方式 对I/O设备的程序轮询的方式,是早期的计算机系统对I/O设备的 ...

  6. php 正则截取文章图片

    preg_match ("<img.*src=[\"](.*?)[\"].*?>",$test,$match); //获取图片 echo $matc ...

  7. ACtiveMQ中间件-消息的接收和发送

    一.发送消息基本步骤 (1)启动ActiveMQ的的activemq.bat批处理文件或BrokerService节点 (2)创建连接使用的工厂类ActiveMQConnectionFactory通过 ...

  8. best performance / best appearance

  9. IP和网段及子网掩码基础知识

    IP地址由网络号和主机号两部分组成,网络号的最高位必须是"0",IP地址和子网掩码求"与"算出网络地址,只有网络地址相同才可直接通信,否则需要借助路由. 主机标 ...

  10. TCP、UDP之三次握手四次挥手

    1. http协议的简介 HTTP,HyperText Transfer Protocol.超文本传输协议,是互联网上应用最为广泛的一种网络协议.基于TCP的协议,HTTP是一个客户端和服务器端请求和 ...