Table per Type (TPT):用外鍵關(guān)系來表示繼承關(guān)系。
我們可以使用單個實體通過實體拆分來扁平化繼承層次中表示TPT皆尔,最終使用數(shù)據(jù)庫中的單表來維護數(shù)據(jù)。換句話說慷蠕,實體拆分涉及將概念層中的單個實體映射到存儲(數(shù)據(jù)庫)層中的多個表榄审。
-
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最終映射會在數(shù)據(jù)庫單表(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");
});
}
}
}
經(jīng)過上面配置后,那么生成的數(shù)據(jù)庫中的表如下:
我們發(fā)現(xiàn)婶熬,它生成了2張表尸诽,并且它們之間通過EmployeeId主外鍵進行關(guān)聯(lián)盯另。
當(dāng)我們對Employee實體執(zhí)行插入操作時,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