一步一步体验 ADO.NET Entity Framework:第3步:理清思路——EDM中的关系和继承

为了让我们对定义EDM有一个清晰的思路,这一篇文章我们来体验一下ADO.NET Entity Framework中的关系和继承,

 

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

 

.一对一的关系

.一对多的关系

.多对多的关系

.其他松散的关系

 

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

.一对一关系:

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

 

 

 

 

EDM代码如下:

 

 一对一关系代码: 

Code

 

 

一对多关系的代码:

 

One to Many 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

posted @ 2008-08-17 13:28  瘦山鸿  阅读(6109)  评论(8编辑  收藏  举报