映射一

Mapping the Table-Per-Hierarchy (TPH) Inheritance

模型文件

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions; namespace DataAnnotations
{
public class TestContext : DbContext
{
public DbSet<Course> Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<Course>()
.Map<Course>(m => m.Requires("Type").HasValue("Course"))
.Map<OnlineCourse>(m => m.Requires("Type").HasValue("OnlineCourse"));
}
}//class public class Course
{
public int CourseID { get; set; } public string Title { get; set; }
public int Credits { get; set; } }//class public partial class OnlineCourse : Course
{
public string URL { get; set; } }//class
}

对应的数据库代码:

CREATE TABLE [dbo].[Course] (
[CourseID] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (MAX) NULL,
[Credits] INT NOT NULL,
[URL] NVARCHAR (MAX) NULL,
[Type] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.Course] PRIMARY KEY CLUSTERED ([CourseID] ASC)
);

此是默认的策略,去掉建造者中的配置信息,代码如下:

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions; namespace DataAnnotations
{
public class TestContext : DbContext
{
public DbSet<Course> Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}//class public class Course
{
public int CourseID { get; set; } public string Title { get; set; }
public int Credits { get; set; } }//class public partial class OnlineCourse : Course
{
public string URL { get; set; } }//class
}

生成的数据库代码:

CREATE TABLE [dbo].[Course] (
[CourseID] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (MAX) NULL,
[Credits] INT NOT NULL,
[URL] NVARCHAR (MAX) NULL,
[Discriminator] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.Course] PRIMARY KEY CLUSTERED ([CourseID] ASC)
);

可见,鉴别器列被命名为Discriminator,其保存记录的类名称。

映射二

Mapping the Table-Per-Type (TPT) Inheritance

模型文件:

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions; namespace DataAnnotations
{
public class TestContext : DbContext
{
public DbSet<Course> Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); //TPT
modelBuilder.Entity<Course>().ToTable("Course");
modelBuilder.Entity<OnlineCourse>().ToTable("OnlineCourse");
}
} public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
} public partial class OnlineCourse : Course
{
public string URL { get; set; }
}
}

将类型名称与表名称对应,生成的数据库代码:

CREATE TABLE [dbo].[Course] (
[CourseID] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (MAX) NULL,
[Credits] INT NOT NULL,
CONSTRAINT [PK_dbo.Course] PRIMARY KEY CLUSTERED ([CourseID] ASC)
); ------------------------------------------------------------------------------------------
CREATE TABLE [dbo].[OnlineCourse] (
[CourseID] INT NOT NULL,
[URL] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.OnlineCourse] PRIMARY KEY CLUSTERED ([CourseID] ASC),
CONSTRAINT [FK_dbo.OnlineCourse_dbo.Course_CourseID] FOREIGN KEY ([CourseID]) REFERENCES [dbo].[Course] ([CourseID])
); GO
CREATE NONCLUSTERED INDEX [IX_CourseID]
ON [dbo].[OnlineCourse]([CourseID] ASC);

派生类对应的表会有一个外键,将派生表的主键与基类对应的主表关联,形成0/1..1的关系。

映射三

Mapping the Table-Per-Concrete Class (TPC) Inheritance

模型:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions; namespace DataAnnotations
{
public class TestContext : DbContext
{
public DbSet<Course> Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); //TPC
modelBuilder.Entity<OnlineCourse>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("OnlineCourse");
});
}
} public class Course
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CourseID { get; set; } public string Title { get; set; }
public int Credits { get; set; }
} public partial class OnlineCourse : Course
{
public string URL { get; set; }
}
}

生成的数据库代码:

CREATE TABLE [dbo].[Course] (
[CourseID] INT NOT NULL,
[Title] NVARCHAR (MAX) NULL,
[Credits] INT NOT NULL,
CONSTRAINT [PK_dbo.Course] PRIMARY KEY CLUSTERED ([CourseID] ASC)
); -----------------------------------------------------------------------------------------
CREATE TABLE [dbo].[OnlineCourse] (
[CourseID] INT NOT NULL,
[Title] NVARCHAR (MAX) NULL,
[Credits] INT NOT NULL,
[URL] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.OnlineCourse] PRIMARY KEY CLUSTERED ([CourseID] ASC)
);

注意,主键在整个继承层次结构上唯一,而不是单个表上唯一。

映射四

Mapping Properties of an Entity Type to Multiple Tables in the Database (Entity Splitting)

模型:

using System;
using System.Data.Entity; namespace DataAnnotations
{
public class TestContext : DbContext
{
public DbSet<Department> Departments { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>()
.Map(m =>
{
m.Properties(t => new { t.DepartmentID, t.Name });
m.ToTable("Department");
})
.Map(m =>
{
m.Properties(t => new { t.DepartmentID, t.StartDate });
m.ToTable("DepartmentDetails");
});
}
} public class Department
{
public int DepartmentID { get; set; }
public string Name { get; set; }
public DateTime StartDate { get; set; }
}
}

生成的数据库代码:

CREATE TABLE [dbo].[Department] (
[DepartmentID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.Department] PRIMARY KEY CLUSTERED ([DepartmentID] ASC)
); ----------------------------------------------------------------------------------------
CREATE TABLE [dbo].[DepartmentDetails] (
[DepartmentID] INT NOT NULL,
[StartDate] DATETIME NOT NULL,
CONSTRAINT [PK_dbo.DepartmentDetails] PRIMARY KEY CLUSTERED ([DepartmentID] ASC),
CONSTRAINT [FK_dbo.DepartmentDetails_dbo.Department_DepartmentID] FOREIGN KEY ([DepartmentID]) REFERENCES [dbo].[Department] ([DepartmentID])
); GO
CREATE NONCLUSTERED INDEX [IX_DepartmentID]
ON [dbo].[DepartmentDetails]([DepartmentID] ASC);

每个表都包含主键,第一个表的主键Identity,第二个表的主键无Identity,外键将两个表的主键关联。

映射五

Mapping Multiple Entity Types to One Table in the Database (Table Splitting)

这个模式主要用于懒惰加载,模型文件:

using System.ComponentModel.DataAnnotations;
using System.Data.Entity; namespace DataAnnotations
{
public class TestContext : DbContext
{
public DbSet<Instructor> Instructors { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Instructor>()
.HasRequired(t => t.OfficeAssignment)
.WithRequiredPrincipal(t => t.Instructor); modelBuilder.Entity<Instructor>().ToTable("Instructor");
modelBuilder.Entity<OfficeAssignment>().ToTable("Instructor");
}
}// TestContext public class Instructor
{
public int InstructorID { get; set; }
public string Name { get; set; } // 导航属性
public OfficeAssignment OfficeAssignment { get; set; }
} public class OfficeAssignment
{
[Key]
public int InstructorID { get; set; }
public string Location { get; set; } // 导航属性
public Instructor Instructor { get; set; }
} }

生成的数据库代码为:

CREATE TABLE [dbo].[Instructor] (
[InstructorID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
[Location] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.Instructor] PRIMARY KEY CLUSTERED ([InstructorID] ASC)
);

Code First: 五大映射模式的更多相关文章

  1. Windows GDI 映射模式(出自:Windows程序设计第5版-珍藏版)

    GDI映射模式(mapping mode):和映射模式紧密相关的还有4个其它的设备环境属性:1.窗口原点(window origin)2.视口原点(viewport origin)3.窗口范围(win ...

  2. Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

  3. php设计模式 数据对象映射模式

    数据对象映射模式,是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作. 在代码中实现数据对象映射模式,实现一个ORM类,将复杂的sql语句映射成对象属性的操作.对象关系映射(Obje ...

  4. MFC坐标空间与映射模式

    逻辑坐标:使用GDI绘图时使用的坐标系 设备坐标系:实际设备(显示器.打印机)的坐标系,即我们实际看到的坐标系. 坐标空间 在Windows NT/2000中Win32 API中支持以下四层坐标空间: ...

  5. PHP 设计模式 笔记与总结(10)数据对象映射模式 2

    [例2]数据对象映射模式结合[工厂模式]和[注册模式]的使用. 入口文件 index.php: <?php define('BASEDIR',__DIR__); //定义根目录常量 includ ...

  6. PHP 设计模式 笔记与总结(9)数据对象映射模式

    [数据对象映射模式] 是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作.例如在代码中 new 一个对象,使用数据对象映射模式就可以将对象的一些操作比如设置一些属性,就会自动保存到数 ...

  7. Shapefile文件中的坐标绘制到屏幕时的映射模式设置

    pDC->SetMapMode(MM_ANISOTROPIC ); //首先选择MM_ANISOTROPIC映射模式,其它映射模式都不合适 pDC->SetWindowExt( max(a ...

  8. Visual C++ 打印编程技术-编程基础-映射模式

    映射模式: Visual C++ 中采用的坐标映射方式使得用户图形坐标和输出设别的像素完全一致. eg:当屏幕的像素大小为800X600时,每英寸包含屏幕像素为96,打印机则需要几倍的点数才能达到同样 ...

  9. PHP设计模式笔记六:数据对象映射模式 -- Rango韩老师 http://www.imooc.com/learn/236

    数据对象映射模式 1.数据对象映射模式,是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作 2.在代码中实现数据对象映射模式,我们将实现一个ORM类,将复杂的SQL语句映射成对象属性 ...

随机推荐

  1. 如何在 CentOS 7 上生成 SSL 证书为 Nginx 加密

    本文首发:开发指南:如何在 CentOS 7 上安装 Nginx Let’s Encrypt 是由 Internet Security Research Group (ISRG) 开发的一个自由.自动 ...

  2. mysql解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)的报错

    一般这个错误是由密码错误引起,解决的办法自然就是重置密码. 假设我们使用的是root账户. 1.重置密码的第一步就是跳过MySQL的密码认证过程,方法如下: #vim /etc/my.cnf(注:wi ...

  3. centos6基础优化

    一.关闭SELinux功能 selinux功能太严苛,还是关闭了吧 法一:修改配置文件,永久生效 [root@web01 ~]# sed -i 's/SELINUX=enforcing/SELINUX ...

  4. SPOJ GSS6 Can you answer these queries VI

    Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...

  5. Installing Zabbix 3.2 in Centos 6.8 Clean Install Dependencies Errors

    ZABBIX Forums > Zabbix Discussions and Feedback > Zabbix Troubleshooting and Problems > Ins ...

  6. mongodb shell 无法删除问题

    1.MongoDB Shell中退格键使用的问题. 利用SecureCRT工具访问linux的时候,在使用MongoDB的交互式shell的时候,退格键(Backspace)无法使用,导致无 法修改输 ...

  7. markman & psd

    markman & psd MarkMan 设计稿标 & 测量神器 http://www.getmarkman.com/ https://www.jianshu.com/p/83af3 ...

  8. [SPOJ8222]Substrings

    [SPOJ8222]Substrings 试题描述 You are given a string S which consists of 250000 lowercase latin letters ...

  9. HDU 4944

    FSF’s game Problem Description FSF has programmed a game.In this game, players need to divide a rect ...

  10. [USACO12FEB]附近的牛Nearby Cows

    题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into accoun ...