每个类型一张表【TPT】

声明方式

   public class Business
{
[Key]
public int BusinessId { get; protected set; }
public string Name { get; set; }
public string LicenseNumber { get; set; }
}
public class Retail : Business
{
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZIPCode { get; set; }
}
public class eCommerce : Business
{
public string URL { get; set; }
}public class BusinessesContext : DbContext
{
public DbSet<Business> Businesses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Business>()
.Property(b=>b.BusinessId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Business>().ToTable("Business", "halower");
modelBuilder.Entity<Retail>().ToTable("Retail", "halower");
modelBuilder.Entity<eCommerce>().ToTable("eCommerce", "halower");
}
}

怎么使用

 private static void Main(string[] args)
{
using (var context = new BusinessesContext())
{ var retail = new Retail
{
Name = "Shop and Save",
LicenseNumber = "200C",
Address = "101 Main",
City = "Anytown",
State = "TX",
ZIPCode = ""
};
context.Businesses.Add(retail);
var web = new eCommerce
{
Name = "BuyNow.com",
LicenseNumber = "300AB",
URL = "www.buynow.com"
};
context.Businesses.Add(web);
context.SaveChanges();
}
using (var context = new BusinessesContext())
{
Console.WriteLine("\n--- All Businesses ---");
foreach (var b in context.Businesses)
{
Console.WriteLine("{0} (#{1})", b.Name, b.LicenseNumber);
}
Console.WriteLine("\n--- Retail Businesses ---");
//OfType<T>:根据指定类型筛选
foreach (var r in context.Businesses.OfType<Retail>())
{
Console.WriteLine("{0} (#{1})", r.Name, r.LicenseNumber);
Console.WriteLine("{0}", r.Address);
Console.WriteLine("{0}, {1} {2}", r.City, r.State, r.ZIPCode);
}
Console.WriteLine("\n--- eCommerce Businesses ---");
foreach (var e in context.Businesses.OfType<eCommerce>())
{
Console.WriteLine("{0} (#{1})", e.Name, e.LicenseNumber);
Console.WriteLine("Online address is: {0}", e.URL);
}
Console.ReadKey();
}
}

生成表结构

运行效果

每个继承层次一张表【TPH】

声明方式

  public abstract class Employee
{
public int EmployeeId { get; protected set; }
public string FirstName { get; set; }
public string LastName { get; set; }
} public class FullTimeEmployee : Employee
{
public decimal? Salary { get; set; }
} public class HourlyEmployee : Employee
{
public decimal? Wage { get; set; }
} public class EmployeeContext: DbContext
{
public DbSet<Employee> Employees { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Employee>()
.HasKey(e => e.EmployeeId)
.Property(e => e.EmployeeId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Employee>()
.Map<FullTimeEmployee>(m => m.Requires("EmployeeType").HasValue())
.Map<HourlyEmployee>(m => m.Requires("EmployeeType").HasValue());
}

怎么使用

      private static void Main(string[] args)
{
using (var context = new EmployeeContext())
{
var fte = new FullTimeEmployee
{
FirstName = "Jane",
LastName = "Doe",
Salary = 71500M
};
context.Employees.Add(fte);
fte = new FullTimeEmployee
{
FirstName = "John",
LastName = "Smith",
Salary = 62500M
};
context.Employees.Add(fte);
var hourly = new HourlyEmployee
{
FirstName = "Tom",
LastName = "Jones",
Wage = 8.75M
};
context.Employees.Add(hourly);
context.SaveChanges();
}
using (var context = new EmployeeContext())
{
Console.WriteLine("--- All Employees ---");
foreach (var emp in context.Employees)
{
bool fullTime = !(emp is HourlyEmployee);
Console.WriteLine("{0} {1} ({2})", emp.FirstName, emp.LastName,
fullTime ? "Full Time" : "Hourly");
}
Console.WriteLine("--- Full Time ---");
foreach (var fte in context.Employees.OfType<FullTimeEmployee>())
{
Console.WriteLine("{0} {1}", fte.FirstName, fte.LastName);
}
Console.WriteLine("--- Hourly ---");
foreach (var hourly in context.Employees.OfType<HourlyEmployee>())
{
Console.WriteLine("{0} {1}", hourly.FirstName, hourly.LastName); }
}
Console.ReadKey();
}

生成表结构

运行效果

每个子类一张表【TPC】

声明方式

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Employee>()
.HasKey(e => e.EmployeeId)
.Property(e => e.EmployeeId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<FullTimeEmployee>()
.Map(m =>
{
m.MapInheritedProperties();
m.ToTable("FullTimeEmployee");
});
modelBuilder.Entity<HourlyEmployee>()
.Map(m =>
{
m.MapInheritedProperties();
m.ToTable("HourlyEmployee");
});
}

生成表结构

继承映射关系 TPH、TPT、TPC<EntityFramework6.0>的更多相关文章

  1. EF里的继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子

    本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discr ...

  2. entity framework里的继承映射关系TPH、TPT和TPC

    本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discr ...

  3. EF——继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子 05 (转)

    EF里的继承映射关系TPH.TPT和TPC的讲解以及一些具体的例子   本章节讲解EF里的继承映射关系,分为TPH.TPT.TPC.具体: 1.TPH:Table Per Hierarchy 这是EF ...

  4. hibernate 继承映射关系( SINGLE_TABLE)

    三种继承映射关系.   1,SINGLE_TABLE   person student  teacher 在一个表中,student和teacher继承自person,通过一个Discriminato ...

  5. hibernate笔记--继承映射关系的三种实现方式

    单表继承映射(一张表): 假设我们现在有三个类,关系如下: Person类有两个子类Student和Teacher,并且子类都具有自己独有的属性.这种实体关系在hibernate中可以使用单表的继承映 ...

  6. Code First 中的 TPH TPT TPC

    public class Blog { public int Id { get; set; } public DateTime Creationdate { get; set; } public st ...

  7. 继承映射关系 joinedsubclass的查询

    会出现下面这样的错一般是配置文件中的mapping和映射文件中的package路径或者class中的name路径不一致 org.hibernate.MappingException: Unknown ...

  8. 继承映射关系 subclass的查询

    Person大类的映射文件配置 1 <hibernate-mapping package="com.zh.hibernate.subclass"> <class ...

  9. hibernate 继承映射关系( JOINED)

    一个主表,其他的表每个都有自己的表来装填自己特有的部分,共同的部分就放在主表中.   package com.bjsxt.hibernate; import javax.persistence.Ent ...

随机推荐

  1. iOS 中的 promise 模式

    1.概述 异步编程 App 开发中用得非常频繁,但异步请求后的操作却比较麻烦.Promise 就是解决这一问题的编程模型.其适用于 延迟(deferred) 计算和 异步(asynchronous)  ...

  2. 美团HD(4)-二级联动效果

    DJNavDropView.m #import "DJNavDropView.h" #import "DJCategory.h" #import "D ...

  3. CJCMS系列--持久层对MangoDB的支持

    持久层添加对MangoDB数据库的支持 using System; using System.Collections.Generic; using System.Linq; using System. ...

  4. 第3月第21天 nsclassfromstring返回null SVN报错:clean the working copy and then retry the operation

    1. xcodeproj工程损坏时,.m文件没有加入编译. 2. SVN报错:clean the working copy and then retry the operation http://bl ...

  5. Google的Protobuf协议分析

    protobuf和thrift类似,也是一个序列化的协议实现,简称PB(下文出现的PB代表protobuf). Github:https://github.com/google/protobuf 上图 ...

  6. rpc-1-OSI模型

    rpc-1-OSI模型 第一部分,网络7层协议 1. OSI模型: 开放通信系统互联网参考模型,是国际标准化组织(ISO),提出的一个,试图使各种计算机在世界范围内互连为网络的模式.(遵循这个模式,计 ...

  7. C和指针 第十五章 错误报告perror和exit

    15.1 错误报告 perror 任何一种程序都存在出错的可能,包括系统的函数库,当出现错误时,系统提示发生错误,标准库函数在一个外部整型变量中保存错误代码,然后把错误代码传给用户程序,提示错误原因. ...

  8. Jni :三维数组处理方法 ,以整形三维数组为例 C++实现

    本文原创,转载请注明地址:http://www.cnblogs.com/baokang/p/4982846.html 关于Jni的基本使用方法,请参阅:Java 调用 C++ (Java 调用 dll ...

  9. PYTHON lambda表达式

    lambda相当于def定义函数     一一对应

  10. Qt-为应用程序添加logo

    在Qt Creator中新建Qt Resource File,名字为logo.qrc 1.选择Add Prefix得到/new/prefix1 2.然后Add Files,将文件添加进去,如CA-DC ...