一步一步体验 ADO.NET Entity Framework:第三步

理清思路——EDM中的关系和继承

关系:在我们的现实生活中关系随处可见,父子关系、主从关系等,在数据驱动的应用系统中,我们更是对关系有了更为深入的认识和研究,那么在我们熟悉的关系型数据应用系统中,关系的类型也不过如下几种:



.一对一的关系

.一对多的关系

.多对多的关系

.其他松散的关系



那么在EDM中这些关系是如何定义的呢?我们来看看以上关系的EDM实现:

.一对一关系:

比如我们假定要给单位实体和货品实体定义一对一的关系,如图:



 附件: 您所在的用户组无法下载或查看附件


 附件: 您所在的用户组无法下载或查看附件


 附件: 您所在的用户组无法下载或查看附件


EDM代码如下:



一对一关系代码:



Code





一对多关系的代码:

One to Many Code


Code





多对多关系代码:





Code





关于关系的更多详细信息,参看msdn(英文)

http://msdn.microsoft.com/en-us/library/bb399566.aspx

http://msdn.microsoft.com/en-us/library/bb399277.aspx

可以帮助理解。



继承:我们来看看ms对继承在EDM是怎么描述的:



Inheritance (EDM)


In the Entity Data Model (EDM), inheritance permits a derived type to extend the features of another type.

Typically, the model used by an application includes many different types. Some entity types model separate concepts, such as customer and order in a business application. These data types do not share any members, but there are other types that are more similar to each other. For example, consider the customer and employee types. Although these types model different concepts, there is underlying commonality: Both represent people involved in business relationships and both include properties to store information such as name, address, and telephone number.

EDM inheritance permits one type to be derived from another type. For example, Employee and Customer can both inherit from the Contact type. In this case, Contact is called the base type. Employee and Customer are called derived types.

Inheritance does not have to stop at a single level: It is possible for a derived type to be the base type of another entity type. For example, Employee is a base type of Manager; Customer is a base type of PreferredCustomer, and so on. In this manner, inheritance hierarchies are built.

Like the common language runtime (CLR), the EDM system supports only single inheritance of data types. An entity type can only directly inherit properties from one super-type.



这一段Like the common language runtime (CLR), the EDM system supports only single inheritance of data types. An entity type can only directly inherit properties from one super-type. 值得注意哦。



我们来看看实现的效果如图:


 附件: 您所在的用户组无法下载或查看附件





其实就是一个从基实体派生的过程,代码如下:





Code




MSDN的参考信息连接如下:

http://msdn.microsoft.com/en-us/library/bb399159.aspx

http://msdn.microsoft.com/en-us/library/cc716744.aspx


(文/alcome  出处/博客园)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [ADO.Net] 的这些文章也感兴趣:

ADO.NET Entity Framework:lazy loading(一)
ADO.NET Entity Framework(4)ObjectQuery
DataGrid如何绑定一个超级链接列
ADO.NET Entity Framework(5)esql
DataTable.Select使用小心得
ADO.NET Entity Framework支持多Provider
如何把一个DataTable中的某列通过表关系加到另外一个DataTable中
ADO.NET 数据库访问之数据分页
在ADO.NET数据集中浏览多个相关表(6)
Digging into the Performance of the ADO.NET Entity Framework