Table per Type (TPT):用外键关系来表示继承关系。
我们可以使用单个实体通过实体拆分来扁平化继承层次中表示TPT,最终使用数据库中的单表来维护数据。换句话说,实体拆分涉及将概念层中的单个实体映射到存储(数据库)层中的多个表。
-
Entity Splitting (实体拆分)
我们定义如下雇员类,其中EmployeeId、Name、CreateTime、ModifiedTime属性作为Employye主表字段,而PhoneNumber和DetailAddress作为EmployeeDetail详细表字段。
using System;
namespace EntityFramework_CreateDbContext.Entity
{
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string DetailAddress { get; set; }
public DateTime CreateTime { get; set; }
public DateTime ModifiedTime { get; set; }
}
}
在默认配置下,EF最终映射会在数据库单表(Employee)中生成所有的列,但是我们需要两个表来存储这些信息,所以通过继承自DbContext类的OnModelCreating方法来改变这个配置。
using System.Data.Entity.ModelConfiguration;
using EntityFramework_CreateDbContext.Entity;
namespace EntityFramework_CreateDbContext.Map
{
public class EmployeeMap:EntityTypeConfiguration<Employee>
{
public EmployeeMap()
{
Map(map =>
{
map.Properties(p=>new
{
p.EmployeeId,
p.Name,
p.CreateTime,
p.ModifiedTime
});
map.ToTable("Employees");
}).Map(map =>
{
map.Properties(p=>new
{
p.PhoneNumber,
p.DetailAddress
});
map.ToTable("EmployeeDetails");
});
}
}
}
经过上面配置后,那么生成的数据库中的表如下:
我们发现,它生成了2张表,并且它们之间通过EmployeeId主外键进行关联。
当我们对Employee实体执行插入操作时,EF会生成Employees和EmployeeDetails两个表的插入语句。
db.Database.Log = Console.WriteLine;
var employess=new Employee()
{
CreateTime = DateTime.Now,
ModifiedTime = DateTime.Now,
Name = "杨钰涵",
PhoneNumber = "189********",
DetailAddress = "中国"
};
db.Employees.Add(employess);
db.SaveChanges();
控制台输出sql语句:
Opened connection at 2019/3/24 5:28:54 +08:00
SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE t.TABLE_SCHEMA + '.' + t.TABLE_NAME IN ('dbo.Customers','dbo.Orders','dbo.Employees','dbo.Courses','dbo.StudentCourses','dbo.Students','dbo.StudentContacts','dbo.StudentInfos','dbo.EmployeeDetails')
OR t.TABLE_NAME = 'EdmMetadata'
-- Executing at 2019/3/24 5:28:54 +08:00
-- Completed in 43 ms with result: 9
Closed connection at 2019/3/24 5:28:54 +08:00
Opened connection at 2019/3/24 5:28:54 +08:00
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE [Extent1].[ContextKey] = @p__linq__0
) AS [GroupBy1]
-- p__linq__0: 'EntityFramework_CreateDbContext.EfDbContext' (Type = String, Size = 4000)
-- Executing at 2019/3/24 5:28:54 +08:00
-- Completed in 44 ms with result: SqlDataReader
Closed connection at 2019/3/24 5:28:55 +08:00
Opened connection at 2019/3/24 5:28:55 +08:00
SELECT TOP (1)
[Project1].[C1] AS [C1],
[Project1].[MigrationId] AS [MigrationId],
[Project1].[Model] AS [Model],
[Project1].[ProductVersion] AS [ProductVersion]
FROM ( SELECT
[Extent1].[MigrationId] AS [MigrationId],
[Extent1].[Model] AS [Model],
[Extent1].[ProductVersion] AS [ProductVersion],
1 AS [C1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE [Extent1].[ContextKey] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC
-- p__linq__0: 'EntityFramework_CreateDbContext.EfDbContext' (Type = String, Size = 4000)
-- Executing at 2019/3/24 5:28:55 +08:00
-- Completed in 20 ms with result: SqlDataReader
Closed connection at 2019/3/24 5:28:55 +08:00
Opened connection at 2019/3/24 5:28:55 +08:00
Started transaction at 2019/3/24 5:28:55 +08:00
INSERT [dbo].[Employees]([Name], [CreateTime], [ModifiedTime])
VALUES (@0, @1, @2)
SELECT [EmployeeId]
FROM [dbo].[Employees]
WHERE @@ROWCOUNT > 0 AND [EmployeeId] = scope_identity()
-- @0: '杨钰涵' (Type = String, Size = -1)
-- @1: '2019/3/24 5:28:53' (Type = DateTime2)
-- @2: '2019/3/24 5:28:53' (Type = DateTime2)
-- Executing at 2019/3/24 5:28:55 +08:00
-- Completed in 16 ms with result: SqlDataReader
INSERT [dbo].[EmployeeDetails]([EmployeeId], [PhoneNumber], [DetailAddress])
VALUES (@0, @1, @2)
-- @0: '1' (Type = Int32)
-- @1: '189********' (Type = String, Size = -1)
-- @2: '中国' (Type = String, Size = -1)
-- Executing at 2019/3/24 5:28:55 +08:00
-- Completed in 19 ms with result: 1
Committed transaction at 2019/3/24 5:28:55 +08:00
Closed connection at 2019/3/24 5:28:55 +08:00