Entity Framwork4.1RC 多种映射关系实例
时间:2011-04-03 来源:小戴博客
前言
在上一篇中,由小戴为大家简单介绍了。EntityFramwork4.1RC(简称EF)提供的新的开发模式。CodeFirst。今天,我将继续为大家讲解EF.并且做一个小的Demo来讲解关系数据库中一对一,一对多,多对多在EF中的映射。另外还将接受继承映射。在EF中暂且不支持枚举映射。我感觉这点还是不如NH。但EF对Linq的支持还是很到位的。再一个性能相比NH还是有一定的优势。OK。让我们开始EF之旅吧。
提纲
1.类设计
UserInfo 1对1 UserLogin
UserInfo 1对多 News
News 多对多 Column
2.映射关系
UserLogin映射
UserInfo映射
总结
最后补充一下,继承映射。如下
Table Per Hierarchy (TPH)
TPH = “Store all my data in one table and use the values from one or more columns to identify which type each row is”
Simple TPH is the default mapping for an inheritance hierarchy.
TPH with custom discriminator column name and values:
modelBuilder.Entity<Product>()
.Map<Product>(m => m.Requires("Type").HasValue("Current"))
.Map<DiscontinuedProduct>(m => m.Requires("Type").HasValue("Old"));
Table Per Type (TPT)
TPT = “store all the data for properties on the base type in a single table, store any additional data for derived types in an extra table that has a foreign key to the base table”
modelBuilder.Entity<Product>().ToTable("Products");
modelBuilder.Entity<DiscontinuedProduct>().ToTable("OldProducts");
Table Per Concrete Class (TPC)
TPC = “Create a completely separate table for each non-abstract type in my hierarchy”
modelBuilder.Entity<Product>().ToTable("Products");
modelBuilder.Entity<DiscontinuedProduct>()
.Map(m =>
{
m.MapInheritedProperties();
m.ToTable("OldProducts");
});
希望对大家有所帮助