本章节讲解EF里的继承映射关系,分为TPH、TPT、TPC。具体:
1.TPH:Table Per Hierarchy
这是EF的默认的继承映射关系:一张表存放基类和子类的所有列,自动生成的discriminator列用来区分基类和子类的数据。新建一个度假村Resort实体类试试:
/// <summary>
/// 度假村类
/// </summary>
public class Resort : Lodging //这里继承了Lodging类
{
public string Entertainment { get; set; } //娱乐
public string Activities { get; set; } //活动
}
之前的住宿类Lodging里有个属性IsResort表示是否度假胜地,现在可以注释掉了,有新的类Resort来继承Lodging表示是否是度假胜地了,跑下程序最终会生成一张表:
并没有生成Resorts表,而是把Resrot实体类里的属性生成到了Lodgings表里。多了一列discriminator,这个是默认的,用来表示数据来自哪个类,继续添加一个插入Lodging表数据的方法:
private static void InsertLodging()
{
var lodging = new CodeFirst.Model.Lodging
{
Name = "Rainy Day Motel",
Destination = new CodeFirst.Model.Destination
{
Name = "Seattle, Washington",
Country = "USA"
}
};
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
context.Lodgings.Add(lodging);
context.SaveChanges();
}
}
再添加一个插入Resort表数据的方法:
private static void InsertResort()
{
var resort = new CodeFirst.Model.Resort
{
Name = "Top Notch Resort and Spa",
MilesFromNearestAirport = ,
Activities = "Spa, Hiking, Skiing, Ballooning",
Destination = new CodeFirst.Model.Destination
{
Name = "Stowe, Vermont",
Country = "USA"
}
};
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
context.Lodgings.Add(resort);
context.SaveChanges();
}
}
在Main方法里调用两个插入方法,可得到如下数据:
两个插入的数据都到了一张表里。Discriminator列表示数据来自哪一列。当然是可以配置的,这里就必须使用Fluent API配置了,Data Annotation表示无能为力,到LodgingMap里进行配置:
this.Map<CodeFirst.Model.Lodging>(l => { l.Requires("From").HasValue("Standard"); });
this.Map<CodeFirst.Model.Resort>(l => { l.Requires("From").HasValue("Resort"); });
生成了我们指定的From列,数据Standard、Resort分别表示来自Lodging和Resrot表,形象点就是1号酒店是普通酒店,2号就是是度假胜地的酒店:
当然,这里甚至可以把HasValue方法里的参数设置成True和False,用布尔类型的数据区分普通酒店和度假胜地的酒店更形象,园友lk8167给了一个更形象的普通售货员和销售经理的例子
2.TPT:Table Per Type
父类和子类在不同的表里。使用Data Annotation配置TPT:
[Table("Resorts")]
public class Resort : Lodging
{
public string Entertainment { get; set; }
public string Activities { get; set; }
}
或者使用Fluent API配置:
this.Map(m =>
{
m.ToTable("Lodgings");
}).Map<CodeFirst.Model.Resort>(m =>
{
m.ToTable("Resorts");
});
注意:上面配置TPH的Fluetn API需要注释掉在跑程序,那是测试TPH的配置。同时释放这句的注释:context.Database.Initialize(true);,这里没修改实体,但是也需要重新生成数据库。最终数据库是这样的:
父类和子类实体都有一张表,子表通过主键LodgingId找到父表:
3.TPC:Table Per Concrete Type
为每个子类建立一个表,每个与子类对应的表中包含基类的属性对应的列和子类特有属性对应的列。同样之前配置TPT的Fluent API需要先注释掉,然后我们通过Fluent API配置下TPC,TPC也无法用Data Annotation配置:
this.Map(m =>
{
m.ToTable("Lodgings");
}).Map<CodeFirst.Model.Resort>(m =>
{
m.ToTable("Resorts");
m.MapInheritedProperties();
});
生成的数据库:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX0AAAG6CAIAAABr9gEKAAAgAElEQVR4nO2de5AV1Z3HZ/+2rGyljFoWCTExVlFSlWwp7nKTuBHdLVbNo6KbSpGspmaT9h11MFSxY6xdUVwHwjTgsAgiMAziMAQMYoM6jg9ACYLIS8SLEhheM87glIPME3r/6Hu7z+N3+vZ9nXvmzPdTt4qh5/bp0z23P/d3zu3+3iofAAA00t/fX1XpPgAARhfl9c6f1nw2a3XnrNWdT73Q+T+NHdOePXXP0yfvmH3i1ieOT37k2D9Pa7/uD+2JGvKcqpSb9n3fT7upKscrX5djNq0g7aZyPAMAwFF27zRu9pe8cX7ha8PzvKFZLw7OaBmofb7/4WVf3rfoyzv/78vrHtybqKESeKfQFeEdAEpNHt559bXWuRSvvtaqWuVPaz6bvbrzqebOPzZ2PPjMqer6E7c9c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LAFBBdHtn3KSaeO+sWlI30NMWjLP6erLGOTpr+FDt4N7f9+2o7t18W0/rLbueuZT0juemsvWO60SKQb0DgEFo8s7g0FDwGDepJvyZ9M6KRTND73DS2V/Tt6P6zNYpPa23nGy+5t36C+6p/gW5uYxdMoWO47qY3wHALPR55+b/XMU+VN5Z2jBjoKctqHSGTi2WpdP90g/bm656o+6CX/18srw6/8UIYYGDegcAg9Dkne7uruAxblJN+LPKO/2nvaHulkA6Q58+Fk7oBNLpaJlw9Llvb3jsKzdNSpGbCx2Tcl1H+DgLszsAGIAm77RnGTepJvyZ9M7COY/0n/aGTi0ePt4gSKdrw/UdLRPam646tGBMc+1FqavHk5sLppNd12W+ngWVDgAGods7LKR3Guqmn+1qDqXT98HdZ7ZO6Wn7aSidw4vGHnC/tnTaZeOvHEtuLvs9Bey3MsA7ABiEcdfvzHtyWs/hxs6dDxzdNDm99tqDzeP3LP/Ouwu/2Tpn7NoZX2959NLm2ouWTrtswUPfuHzMxeTmPIfNT8fn6AAYh3HXK9c//vAT0++ddt8dv/v1z269+fofTfzed8d96/IxF1/y1QvlR6U7CwAoBOO8AwCwHngHAKAbeAcAoBt4BwCgG3gHAKAbeAcAoBt4BwCgG3gHAKCb/v7+/wcf6XZz4OMy9gAAAABJRU5ErkJggg==" alt="" />
可见,子类Resorts类也有了基类的所有属性。
注意:为了方便测试生成TPC,我注释了所有Lodging表的导航属性,主要是和Destination的一对多关系、Destination类也需要注释掉Lodging属性和Fluent API关系配置,否则程序跑起来会报DataException错:
An exception occurred while initializing the database. See the InnerException for details.
大家下载demo使用的时候也需要先注释掉Lodging类的导航属性。当然不注释想保留也可以,必须设置外键为可空类型,具体请参考Programming Entity Framework: Code First 第五章 Avoiding Mapping Exceptions with TPC
讲了这几种方式配置继承映射,实际项目中应该用哪个呢?
- 不推荐使用TPC(Type Per Concrete Type),因为在TPC方式中子类中包含的其他类的实例或实例集合不能被映射为表之间的关系。你必须通过手动地在类中添加依赖类的主键属性,从而让 Code First感知到它们之间的关系,而这种方式是和使用Code First的初衷相反的;
- 从查询性能上来说,TPH会好一些,因为所有的数据都存在一个表中,不需要在数据查询时使用join;
- 从存储空间上来说,TPT会好一些,因为使用TPH时所有的列都在一个表中,而表中的记录不可能使用所有的列,于是有很多列的值是null,浪费了很多存储空间;
- 从数据验证的角度来说,TPT好一些,因为TPH中很多子类属性对应的列是可为空的,就为数据验证增加了复杂性。
本系列文章结束,主要讲解了EF里如何使用Code First的方式配置数据库,基本上都是手写的配置,其实大家可能已经想到会有工具可以自动配置这些关系了,对了,就是EF Power Tools。这个工具相当智能,可以直接配置出所有的关系。不过个人还是建议关系不多的话自己手写Fluent API来配置。
另外,前面配置了那么长时间的一对多、多对多等各种关系。配置好了如何用EF对这些数据进行增查改查呢?后续还会有系列文章讲解EF是如何操作数据库的,请保持关注。