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. No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal

    No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in t ...

  2. .net利用NPOI生成excel文件

    整理代码,这个是生成excel文件,用的是HSSF的方式,只能生成65535行,256列的数据,如果要看office07之后的生成,之前的随笔里提过.这个是一个完整的过程. 首先是已经查找好的数据,这 ...

  3. 找不到类SimpleJdbcTemplate ParameterizedRowMapper cannot be resolved

    找不到类SimpleJdbcTemplate 背景 想编译个web应用,原来spring-jdbc.jar用的是Spring 3.1,今天改成用Spring 4.3,报了这个错误. 现象 编译不通过, ...

  4. @Html.EditorFor() 用法

    @Html.EditorFor()返回一个由表达式表示的对象中的每个属性所对应的input元素,主要是针对强类型,一般这种方式用得多些a.@Html.EditorFor(mode=>mode.N ...

  5. SpringMVC学习八 @ResponseBody注解

    (一)在方法上只有@RequestMapping 时,无论方法返回值是什么认为需要跳转,代码实例如下 @RequestMapping("demo10") public People ...

  6. NOIP水题测试(2017082301)

    你们从题目也能看出来今天的题是很水的. 前几期答案还没出,效率有点低,谅解,谅解. 今天的答案应该会出的很快. 下面给题目: 时间限制:3小时 题目一:旅行家的预算 题目二:进制转换 题目三:乘积最大 ...

  7. System.load()与System.loadLibrary()

    Java load 和 loadlibrary方法的区别 1.  相同点: 两个方法都是用来装载dll文件,不论是JNI库文件还是非JNI库文件.本地方法在被调用时都需要通过这两发方法之一将其加载至内 ...

  8. python的数字图像处理学习(2)

    图像的重定义大小,图像的缩扩,图像的旋转: from skimage import transform,data import matplotlib.pyplot as plt img = data. ...

  9. sql在最后一行添加合计

    select nvl(sno,'合计') sno,sum(score) score from sc group by rollup(sno);

  10. windows下如何修改mysql的端口号