一步一步体验 ADO.NET Entity Framework:第3步:理清思路——EDM中的关系和继承
为了让我们对定义EDM有一个清晰的思路,这一篇文章我们来体验一下ADO.NET Entity Framework中的关系和继承,
关系:在我们的现实生活中关系随处可见,父子关系、主从关系等,在数据驱动的应用系统中,我们更是对关系有了更为深入的认识和研究,那么在我们熟悉的关系型数据应用系统中,关系的类型也不过如下几种:
.一对一的关系
.一对多的关系
.多对多的关系
.其他松散的关系
那么在EDM中这些关系是如何定义的呢?我们来看看以上关系的EDM实现:
.一对一关系:
比如我们假定要给单位实体和货品实体定义一对一的关系,如图:
EDM代码如下:
一对一关系代码:
1<?xml version="1.0" encoding="utf-8"?>
2<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
3 <!-- EF Runtime content -->
4 <edmx:Runtime>
5 <!-- SSDL content -->
6 <edmx:StorageModels>
7 <Schema Namespace="BASE.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
8 <EntityContainer Name="BASEStoreContainer">
9 <EntitySet Name="B_GOODS" EntityType="BASE.Store.B_GOODS" store:Type="Tables" Schema="dbo" />
10 <EntitySet Name="B_UNIT" EntityType="BASE.Store.B_UNIT" store:Type="Tables" Schema="dbo" />
11 </EntityContainer>
12 <EntityType Name="B_GOODS">
13 <Key>
14 <PropertyRef Name="GOODSID" />
15 </Key>
16 <Property Name="GOODSID" Type="uniqueidentifier" Nullable="false" />
17 <Property Name="GOODSCODE" Type="nvarchar" MaxLength="50" />
18 <Property Name="GOODSNAME" Type="nvarchar" MaxLength="50" />
19 <Property Name="GOODSSPEC" Type="nvarchar" MaxLength="50" />
20 <Property Name="UINTID" Type="uniqueidentifier" />
21 </EntityType>
22 <EntityType Name="B_UNIT">
23 <Key>
24 <PropertyRef Name="UNITID" />
25 </Key>
26 <Property Name="UNITID" Type="uniqueidentifier" Nullable="false" />
27 <Property Name="UNITNAME" Type="nchar" MaxLength="10" />
28 <Property Name="UNITIDP" Type="uniqueidentifier" />
29 <Property Name="UNITGENE" Type="int" Nullable="false" />
30 </EntityType>
31 </Schema></edmx:StorageModels>
32 <!-- CSDL content -->
33 <edmx:ConceptualModels>
34 <Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm" Namespace="BASE" Alias="Self">
35 <EntityContainer Name="BASEContainer" >
36 <EntitySet Name="B_UNIT集" EntityType="BASE.B_UNIT" />
37 <EntitySet Name="B_GOODS集" EntityType="BASE.B_GOODS" />
38 <AssociationSet Name="B_UNITB_GOODS" Association="BASE.B_UNITB_GOODS">
39 <End Role="B_UNIT" EntitySet="B_UNIT集" />
40 <End Role="B_GOODS" EntitySet="B_GOODS集" /></AssociationSet></EntityContainer>
41 <EntityType Name="B_UNIT">
42 <Key>
43 <PropertyRef Name="UNITID" />
44 </Key>
45 <Property Name="UNITID" Type="Guid" Nullable="false" />
46 <Property Name="UNITNAME" Type="String" Nullable="false" />
47 <Property Name="UNITIDP" Type="Guid" Nullable="false" />
48 <Property Name="UNITGENE" Type="Int32" Nullable="false" />
49 <NavigationProperty Name="B_GOODS" Relationship="BASE.B_UNITB_GOODS" FromRole="B_UNIT" ToRole="B_GOODS" /></EntityType>
50 <EntityType Name="B_GOODS">
51 <Key>
52 <PropertyRef Name="GOODSID" />
53 </Key>
54 <Property Name="GOODSID" Type="Guid" Nullable="false" />
55 <Property Name="货品编码" Type="String" Nullable="false" />
56 <Property Name="货品名称" Type="String" Nullable="true" />
57 <Property Name="货品规格" Type="String" Nullable="true" />
58 <Property Name="数量单位" Type="Guid" Nullable="true" />
59 <NavigationProperty Name="B_UNIT" Relationship="BASE.B_UNITB_GOODS" FromRole="B_GOODS" ToRole="B_UNIT" /></EntityType>
60 <Association Name="B_UNITB_GOODS">
61 <End Type="BASE.B_UNIT" Role="B_UNIT" Multiplicity="1" />
62 <End Type="BASE.B_GOODS" Role="B_GOODS" Multiplicity="1" /></Association></Schema>
63 </edmx:ConceptualModels>
64 <!-- C-S mapping content -->
65 <edmx:Mappings>
66 <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S">
67 <Alias Key="Model" Value="BASE" />
68 <Alias Key="Target" Value="BASE.Store" />
69 <EntityContainerMapping CdmEntityContainer="BASEContainer" StorageEntityContainer="BASEStoreContainer">
70 <EntitySetMapping Name="B_UNIT集">
71 <EntityTypeMapping TypeName="IsTypeOf(BASE.B_UNIT)">
72 <MappingFragment StoreEntitySet="B_UNIT">
73 <ScalarProperty Name="UNITIDP" ColumnName="UNITIDP" />
74 <ScalarProperty Name="UNITGENE" ColumnName="UNITGENE" />
75 <ScalarProperty Name="UNITNAME" ColumnName="UNITNAME" />
76 <ScalarProperty Name="UNITID" ColumnName="UNITID" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
77 <EntitySetMapping Name="B_GOODS集">
78 <EntityTypeMapping TypeName="IsTypeOf(BASE.B_GOODS)">
79 <MappingFragment StoreEntitySet="B_GOODS">
80 <ScalarProperty Name="数量单位" ColumnName="UINTID" />
81 <ScalarProperty Name="货品规格" ColumnName="GOODSSPEC" />
82 <ScalarProperty Name="货品名称" ColumnName="GOODSNAME" />
83 <ScalarProperty Name="货品编码" ColumnName="GOODSCODE" />
84 <ScalarProperty Name="GOODSID" ColumnName="GOODSID" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
85 <AssociationSetMapping Name="B_UNITB_GOODS" TypeName="BASE.B_UNITB_GOODS" StoreEntitySet="B_UNIT">
86 <EndProperty Name="B_UNIT">
87 <ScalarProperty Name="UNITID" ColumnName="UNITID" /></EndProperty></AssociationSetMapping></EntityContainerMapping>
88 </Mapping>
89 </edmx:Mappings>
90 </edmx:Runtime>
91 <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
92 <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
93 <edmx:Connection>
94 <DesignerInfoPropertySet>
95 <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
96 </DesignerInfoPropertySet>
97 </edmx:Connection>
98 <edmx:Options>
99 <DesignerInfoPropertySet>
100 <DesignerProperty Name="ValidateOnBuild" Value="true" />
101 </DesignerInfoPropertySet>
102 </edmx:Options>
103 <!-- Diagram content (shape and connector positions) -->
104 <edmx:Diagrams>
105 <Diagram Name="BASE" >
106 <EntityTypeShape EntityType="BASE.B_UNIT" Width="1.5" PointX="0.625" PointY="0.5" Height="1.7908333333333335" IsExpanded="true" />
107 <EntityTypeShape EntityType="BASE.B_GOODS" Width="1.5" PointX="3" PointY="0.5" Height="1.959453125" />
108 <AssociationConnector Association="BASE.B_UNITB_GOODS">
109 <ConnectorPoint PointX="2.125" PointY="1.3111067708333335" />
110 <ConnectorPoint PointX="3" PointY="1.3111067708333335" /></AssociationConnector></Diagram></edmx:Diagrams>
111 </edmx:Designer>
112</edmx:Edmx>
一对多关系的代码:
1<?xml version="1.0" encoding="utf-8"?>
2<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
3 <!-- EF Runtime content -->
4 <edmx:Runtime>
5 <!-- SSDL content -->
6 <edmx:StorageModels>
7 <Schema Namespace="BASE.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
8 <EntityContainer Name="BASEStoreContainer">
9 <EntitySet Name="B_GOODS" EntityType="BASE.Store.B_GOODS" store:Type="Tables" Schema="dbo" />
10 <EntitySet Name="B_UNIT" EntityType="BASE.Store.B_UNIT" store:Type="Tables" Schema="dbo" />
11 </EntityContainer>
12 <EntityType Name="B_GOODS">
13 <Key>
14 <PropertyRef Name="GOODSID" />
15 </Key>
16 <Property Name="GOODSID" Type="uniqueidentifier" Nullable="false" />
17 <Property Name="GOODSCODE" Type="nvarchar" MaxLength="50" />
18 <Property Name="GOODSNAME" Type="nvarchar" MaxLength="50" />
19 <Property Name="GOODSSPEC" Type="nvarchar" MaxLength="50" />
20 <Property Name="UINTID" Type="uniqueidentifier" />
21 </EntityType>
22 <EntityType Name="B_UNIT">
23 <Key>
24 <PropertyRef Name="UNITID" />
25 </Key>
26 <Property Name="UNITID" Type="uniqueidentifier" Nullable="false" />
27 <Property Name="UNITNAME" Type="nchar" MaxLength="10" />
28 <Property Name="UNITIDP" Type="uniqueidentifier" />
29 <Property Name="UNITGENE" Type="int" Nullable="false" />
30 </EntityType>
31 </Schema></edmx:StorageModels>
32 <!-- CSDL content -->
33 <edmx:ConceptualModels>
34 <Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm" Namespace="BASE" Alias="Self">
35 <EntityContainer Name="BASEContainer" >
36 <EntitySet Name="B_UNIT集" EntityType="BASE.B_UNIT" />
37 <EntitySet Name="B_GOODS集" EntityType="BASE.B_GOODS" />
38 <AssociationSet Name="B_UNITB_GOODS" Association="BASE.B_UNITB_GOODS">
39 <End Role="B_UNIT" EntitySet="B_UNIT集" />
40 <End Role="B_GOODS" EntitySet="B_GOODS集" /></AssociationSet></EntityContainer>
41 <EntityType Name="B_UNIT">
42 <Key>
43 <PropertyRef Name="UNITID" />
44 </Key>
45 <Property Name="UNITID" Type="Guid" Nullable="false" />
46 <Property Name="UNITNAME" Type="String" Nullable="false" />
47 <Property Name="UNITIDP" Type="Guid" Nullable="false" />
48 <Property Name="UNITGENE" Type="Int32" Nullable="false" />
49 <NavigationProperty Name="B_GOODS" Relationship="BASE.B_UNITB_GOODS" FromRole="B_UNIT" ToRole="B_GOODS" /></EntityType>
50 <EntityType Name="B_GOODS">
51 <Key>
52 <PropertyRef Name="GOODSID" />
53 </Key>
54 <Property Name="GOODSID" Type="Guid" Nullable="false" />
55 <Property Name="货品编码" Type="String" Nullable="false" />
56 <Property Name="货品名称" Type="String" Nullable="true" />
57 <Property Name="货品规格" Type="String" Nullable="true" />
58 <Property Name="数量单位" Type="Guid" Nullable="true" />
59 <NavigationProperty Name="B_UNIT" Relationship="BASE.B_UNITB_GOODS" FromRole="B_GOODS" ToRole="B_UNIT" /></EntityType>
60 <Association Name="B_UNITB_GOODS">
61 <End Type="BASE.B_UNIT" Role="B_UNIT" Multiplicity="1" />
62 <End Type="BASE.B_GOODS" Role="B_GOODS" Multiplicity="*" /></Association></Schema>
63 </edmx:ConceptualModels>
64 <!-- C-S mapping content -->
65 <edmx:Mappings>
66 <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S">
67 <Alias Key="Model" Value="BASE" />
68 <Alias Key="Target" Value="BASE.Store" />
69 <EntityContainerMapping CdmEntityContainer="BASEContainer" StorageEntityContainer="BASEStoreContainer">
70 <EntitySetMapping Name="B_UNIT集">
71 <EntityTypeMapping TypeName="IsTypeOf(BASE.B_UNIT)">
72 <MappingFragment StoreEntitySet="B_UNIT">
73 <ScalarProperty Name="UNITIDP" ColumnName="UNITIDP" />
74 <ScalarProperty Name="UNITGENE" ColumnName="UNITGENE" />
75 <ScalarProperty Name="UNITNAME" ColumnName="UNITNAME" />
76 <ScalarProperty Name="UNITID" ColumnName="UNITID" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
77 <EntitySetMapping Name="B_GOODS集">
78 <EntityTypeMapping TypeName="IsTypeOf(BASE.B_GOODS)">
79 <MappingFragment StoreEntitySet="B_GOODS">
80 <ScalarProperty Name="数量单位" ColumnName="UINTID" />
81 <ScalarProperty Name="货品规格" ColumnName="GOODSSPEC" />
82 <ScalarProperty Name="货品名称" ColumnName="GOODSNAME" />
83 <ScalarProperty Name="货品编码" ColumnName="GOODSCODE" />
84 <ScalarProperty Name="GOODSID" ColumnName="GOODSID" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
85 <AssociationSetMapping Name="B_UNITB_GOODS" TypeName="BASE.B_UNITB_GOODS" StoreEntitySet="B_UNIT">
86 <EndProperty Name="B_UNIT">
87 <ScalarProperty Name="UNITID" ColumnName="UNITID" /></EndProperty></AssociationSetMapping></EntityContainerMapping>
88 </Mapping>
89 </edmx:Mappings>
90 </edmx:Runtime>
91 <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
92 <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
93 <edmx:Connection>
94 <DesignerInfoPropertySet>
95 <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
96 </DesignerInfoPropertySet>
97 </edmx:Connection>
98 <edmx:Options>
99 <DesignerInfoPropertySet>
100 <DesignerProperty Name="ValidateOnBuild" Value="true" />
101 </DesignerInfoPropertySet>
102 </edmx:Options>
103 <!-- Diagram content (shape and connector positions) -->
104 <edmx:Diagrams>
105 <Diagram Name="BASE" >
106 <EntityTypeShape EntityType="BASE.B_UNIT" Width="1.5" PointX="0.625" PointY="0.5" Height="1.7908333333333335" IsExpanded="true" />
107 <EntityTypeShape EntityType="BASE.B_GOODS" Width="1.5" PointX="3" PointY="0.5" Height="1.959453125" />
108 <AssociationConnector Association="BASE.B_UNITB_GOODS">
109 <ConnectorPoint PointX="2.125" PointY="1.3111067708333335" />
110 <ConnectorPoint PointX="3" PointY="1.3111067708333335" /></AssociationConnector></Diagram></edmx:Diagrams>
111 </edmx:Designer>
112</edmx:Edmx>
多对多关系代码:
1<?xml version="1.0" encoding="utf-8"?>
2<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
3 <!-- EF Runtime content -->
4 <edmx:Runtime>
5 <!-- SSDL content -->
6 <edmx:StorageModels>
7 <Schema Namespace="BASE.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
8 <EntityContainer Name="BASEStoreContainer">
9 <EntitySet Name="B_GOODS" EntityType="BASE.Store.B_GOODS" store:Type="Tables" Schema="dbo" />
10 <EntitySet Name="B_UNIT" EntityType="BASE.Store.B_UNIT" store:Type="Tables" Schema="dbo" />
11 </EntityContainer>
12 <EntityType Name="B_GOODS">
13 <Key>
14 <PropertyRef Name="GOODSID" />
15 </Key>
16 <Property Name="GOODSID" Type="uniqueidentifier" Nullable="false" />
17 <Property Name="GOODSCODE" Type="nvarchar" MaxLength="50" />
18 <Property Name="GOODSNAME" Type="nvarchar" MaxLength="50" />
19 <Property Name="GOODSSPEC" Type="nvarchar" MaxLength="50" />
20 <Property Name="UINTID" Type="uniqueidentifier" />
21 </EntityType>
22 <EntityType Name="B_UNIT">
23 <Key>
24 <PropertyRef Name="UNITID" />
25 </Key>
26 <Property Name="UNITID" Type="uniqueidentifier" Nullable="false" />
27 <Property Name="UNITNAME" Type="nchar" MaxLength="10" />
28 <Property Name="UNITIDP" Type="uniqueidentifier" />
29 <Property Name="UNITGENE" Type="int" Nullable="false" />
30 </EntityType>
31 </Schema></edmx:StorageModels>
32 <!-- CSDL content -->
33 <edmx:ConceptualModels>
34 <Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm" Namespace="BASE" Alias="Self">
35 <EntityContainer Name="BASEContainer" >
36 <EntitySet Name="B_UNIT集" EntityType="BASE.B_UNIT" />
37 <EntitySet Name="B_GOODS集" EntityType="BASE.B_GOODS" />
38 <AssociationSet Name="B_UNITB_GOODS" Association="BASE.B_UNITB_GOODS">
39 <End Role="B_UNIT" EntitySet="B_UNIT集" />
40 <End Role="B_GOODS" EntitySet="B_GOODS集" /></AssociationSet></EntityContainer>
41 <EntityType Name="B_UNIT">
42 <Key>
43 <PropertyRef Name="UNITID" />
44 </Key>
45 <Property Name="UNITID" Type="Guid" Nullable="false" />
46 <Property Name="UNITNAME" Type="String" Nullable="false" />
47 <Property Name="UNITIDP" Type="Guid" Nullable="false" />
48 <Property Name="UNITGENE" Type="Int32" Nullable="false" />
49 <NavigationProperty Name="B_GOODS" Relationship="BASE.B_UNITB_GOODS" FromRole="B_UNIT" ToRole="B_GOODS" /></EntityType>
50 <EntityType Name="B_GOODS">
51 <Key>
52 <PropertyRef Name="GOODSID" />
53 </Key>
54 <Property Name="GOODSID" Type="Guid" Nullable="false" />
55 <Property Name="货品编码" Type="String" Nullable="false" />
56 <Property Name="货品名称" Type="String" Nullable="true" />
57 <Property Name="货品规格" Type="String" Nullable="true" />
58 <Property Name="数量单位" Type="Guid" Nullable="true" />
59 <NavigationProperty Name="B_UNIT" Relationship="BASE.B_UNITB_GOODS" FromRole="B_GOODS" ToRole="B_UNIT" /></EntityType>
60 <Association Name="B_UNITB_GOODS">
61 <End Type="BASE.B_UNIT" Role="B_UNIT" Multiplicity="*" />
62 <End Type="BASE.B_GOODS" Role="B_GOODS" Multiplicity="*" /></Association></Schema>
63 </edmx:ConceptualModels>
64 <!-- C-S mapping content -->
65 <edmx:Mappings>
66 <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S">
67 <Alias Key="Model" Value="BASE" />
68 <Alias Key="Target" Value="BASE.Store" />
69 <EntityContainerMapping CdmEntityContainer="BASEContainer" StorageEntityContainer="BASEStoreContainer">
70 <EntitySetMapping Name="B_UNIT集">
71 <EntityTypeMapping TypeName="IsTypeOf(BASE.B_UNIT)">
72 <MappingFragment StoreEntitySet="B_UNIT">
73 <ScalarProperty Name="UNITIDP" ColumnName="UNITIDP" />
74 <ScalarProperty Name="UNITGENE" ColumnName="UNITGENE" />
75 <ScalarProperty Name="UNITNAME" ColumnName="UNITNAME" />
76 <ScalarProperty Name="UNITID" ColumnName="UNITID" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
77 <EntitySetMapping Name="B_GOODS集">
78 <EntityTypeMapping TypeName="IsTypeOf(BASE.B_GOODS)">
79 <MappingFragment StoreEntitySet="B_GOODS">
80 <ScalarProperty Name="数量单位" ColumnName="UINTID" />
81 <ScalarProperty Name="货品规格" ColumnName="GOODSSPEC" />
82 <ScalarProperty Name="货品名称" ColumnName="GOODSNAME" />
83 <ScalarProperty Name="货品编码" ColumnName="GOODSCODE" />
84 <ScalarProperty Name="GOODSID" ColumnName="GOODSID" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
85 <AssociationSetMapping Name="B_UNITB_GOODS" TypeName="BASE.B_UNITB_GOODS" StoreEntitySet="B_UNIT">
86 <EndProperty Name="B_UNIT">
87 <ScalarProperty Name="UNITID" ColumnName="UNITID" /></EndProperty></AssociationSetMapping></EntityContainerMapping>
88 </Mapping>
89 </edmx:Mappings>
90 </edmx:Runtime>
91 <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
92 <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
93 <edmx:Connection>
94 <DesignerInfoPropertySet>
95 <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
96 </DesignerInfoPropertySet>
97 </edmx:Connection>
98 <edmx:Options>
99 <DesignerInfoPropertySet>
100 <DesignerProperty Name="ValidateOnBuild" Value="true" />
101 </DesignerInfoPropertySet>
102 </edmx:Options>
103 <!-- Diagram content (shape and connector positions) -->
104 <edmx:Diagrams>
105 <Diagram Name="BASE" >
106 <EntityTypeShape EntityType="BASE.B_UNIT" Width="1.5" PointX="0.625" PointY="0.5" Height="1.7908333333333335" IsExpanded="true" />
107 <EntityTypeShape EntityType="BASE.B_GOODS" Width="1.5" PointX="3" PointY="0.5" Height="1.959453125" />
108 <AssociationConnector Association="BASE.B_UNITB_GOODS">
109 <ConnectorPoint PointX="2.125" PointY="1.3111067708333335" />
110 <ConnectorPoint PointX="3" PointY="1.3111067708333335" /></AssociationConnector></Diagram></edmx:Diagrams>
111 </edmx:Designer>
112</edmx:Edmx>
关于关系的更多详细信息,参看msdn(英文)
http://msdn.microsoft.com/en-us/library/bb399566.aspx
http://msdn.microsoft.com/en-us/library/bb399277.aspx
可以帮助理解。
继承:我们来看看ms对继承在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. 值得注意哦。
我们来看看实现的效果如图:
其实就是一个从基实体派生的过程,代码如下:
1<?xml version="1.0" encoding="utf-8"?>
2<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
3 <!-- EF Runtime content -->
4 <edmx:Runtime>
5 <!-- SSDL content -->
6 <edmx:StorageModels>
7 <Schema Namespace="BASE.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
8 <EntityContainer Name="BASEStoreContainer">
9 <EntitySet Name="B_GOODS" EntityType="BASE.Store.B_GOODS" store:Type="Tables" Schema="dbo" />
10 <EntitySet Name="B_UNIT" EntityType="BASE.Store.B_UNIT" store:Type="Tables" Schema="dbo" />
11 </EntityContainer>
12 <EntityType Name="B_GOODS">
13 <Key>
14 <PropertyRef Name="GOODSID" />
15 </Key>
16 <Property Name="GOODSID" Type="uniqueidentifier" Nullable="false" />
17 <Property Name="GOODSCODE" Type="nvarchar" MaxLength="50" />
18 <Property Name="GOODSNAME" Type="nvarchar" MaxLength="50" />
19 <Property Name="GOODSSPEC" Type="nvarchar" MaxLength="50" />
20 <Property Name="UINTID" Type="uniqueidentifier" />
21 </EntityType>
22 <EntityType Name="B_UNIT">
23 <Key>
24 <PropertyRef Name="UNITID" />
25 </Key>
26 <Property Name="UNITID" Type="uniqueidentifier" Nullable="false" />
27 <Property Name="UNITNAME" Type="nchar" MaxLength="10" />
28 <Property Name="UNITIDP" Type="uniqueidentifier" />
29 <Property Name="UNITGENE" Type="int" Nullable="false" />
30 </EntityType>
31 </Schema></edmx:StorageModels>
32 <!-- CSDL content -->
33 <edmx:ConceptualModels>
34 <Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm" Namespace="BASE" Alias="Self">
35 <EntityContainer Name="BASEContainer" >
36 <EntitySet Name="B_UNIT集" EntityType="BASE.B_UNIT" />
37 </EntityContainer>
38 <EntityType Name="B_UNIT">
39 <Key>
40 <PropertyRef Name="UNITID" />
41 </Key>
42 <Property Name="UNITID" Type="Guid" Nullable="false" />
43 <Property Name="UNITNAME" Type="String" Nullable="false" />
44 <Property Name="UNITIDP" Type="Guid" Nullable="false" />
45 <Property Name="UNITGENE" Type="Int32" Nullable="false" />
46 </EntityType>
47 <EntityType Name="B_GOODS" BaseType="BASE.B_UNIT">
48 <Property Name="GOODSID" Type="Guid" Nullable="false" />
49 <Property Name="货品编码" Type="String" Nullable="false" />
50 <Property Name="货品名称" Type="String" Nullable="true" />
51 <Property Name="货品规格" Type="String" Nullable="true" />
52 <Property Name="数量单位" Type="Guid" Nullable="true" />
53 </EntityType>
54 </Schema>
55 </edmx:ConceptualModels>
56 <!-- C-S mapping content -->
57 <edmx:Mappings>
58 <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S">
59 <Alias Key="Model" Value="BASE" />
60 <Alias Key="Target" Value="BASE.Store" />
61 <EntityContainerMapping CdmEntityContainer="BASEContainer" StorageEntityContainer="BASEStoreContainer">
62 <EntitySetMapping Name="B_UNIT集">
63 <EntityTypeMapping TypeName="IsTypeOf(BASE.B_UNIT)">
64 <MappingFragment StoreEntitySet="B_UNIT">
65 <ScalarProperty Name="UNITIDP" ColumnName="UNITIDP" />
66 <ScalarProperty Name="UNITGENE" ColumnName="UNITGENE" />
67 <ScalarProperty Name="UNITNAME" ColumnName="UNITNAME" />
68 <ScalarProperty Name="UNITID" ColumnName="UNITID" /></MappingFragment></EntityTypeMapping><EntityTypeMapping TypeName="IsTypeOf(BASE.B_GOODS)">
69 <MappingFragment StoreEntitySet="B_GOODS">
70 <ScalarProperty Name="数量单位" ColumnName="UINTID" />
71 <ScalarProperty Name="货品规格" ColumnName="GOODSSPEC" />
72 <ScalarProperty Name="货品名称" ColumnName="GOODSNAME" />
73 <ScalarProperty Name="货品编码" ColumnName="GOODSCODE" />
74 <ScalarProperty Name="GOODSID" ColumnName="GOODSID" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
75 </EntityContainerMapping>
76 </Mapping>
77 </edmx:Mappings>
78 </edmx:Runtime>
79 <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
80 <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
81 <edmx:Connection>
82 <DesignerInfoPropertySet>
83 <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
84 </DesignerInfoPropertySet>
85 </edmx:Connection>
86 <edmx:Options>
87 <DesignerInfoPropertySet>
88 <DesignerProperty Name="ValidateOnBuild" Value="true" />
89 </DesignerInfoPropertySet>
90 </edmx:Options>
91 <!-- Diagram content (shape and connector positions) -->
92 <edmx:Diagrams>
93 <Diagram Name="BASE" >
94 <EntityTypeShape EntityType="BASE.B_UNIT" Width="1.5" PointX="0.625" PointY="0.5" Height="1.7908333333333335" IsExpanded="true" />
95 <EntityTypeShape EntityType="BASE.B_GOODS" Width="1.5" PointX="3" PointY="0.5" Height="1.959453125" />
96 <InheritanceConnector EntityType="BASE.B_GOODS">
97 <ConnectorPoint PointX="2.125" PointY="1.3111067708333335" />
98 <ConnectorPoint PointX="3" PointY="1.3111067708333335" /></InheritanceConnector></Diagram></edmx:Diagrams>
99 </edmx:Designer>
100</edmx:Edmx>
MSDN的参考信息连接如下:
http://msdn.microsoft.com/en-us/library/bb399159.aspx
http://msdn.microsoft.com/en-us/library/cc716744.aspx