Entity Framework4.0 (四) EF4的内部结构和几个基本概念

前面,大致概述了三种使用EF4构造应用的方法:1.Database First方法。2.Model First 方法。3.Code First 方法。因为是概述,所以没深入也没拓宽。

这次我就按自己的理解去试着揭一揭,EF4的面纱。呵呵,讲述EF4的内部结构。

1. Entity Designer(实体设计器)

Designer提供一个直观的方式,使我们可以创建、修改实体,创建、修改关联,及实体和关联与数据库之间的映射方式。基本上都是鼠标点击、拖拽操作,只有大家多动手,多实践,才能更快更好地使用该设计器。最终在自己的项目中熟练、准确地使用它。

2. ObjectContext(对象上下文):管理与数据库的连接,跟踪、管理对象的状态变化、多线程同步/异步,对象信息同步等。负责把数据库中数据字段映射成内存中对象,并管理这些对象。

3. Entity (实体)和Entity Set(实体集):数据表中的每一行数据,在内存中表现为一个实体(对象)。数据表中的多行数据(如筛选结果为多行),在内存中表现为实体(对象)集合。

4. Scalar Property(标量属性)、Complex Property(复杂属性)、Navigation Property(导航属性):原子属性既是标量属性(如“姓氏”是一个标量属性,“名字”也是一个标量属性)。由多个标量属性组合成的属性叫复杂属性(如“姓氏”和“名字”组合起来成为一个复杂属性“姓名”。)在设计实体时,我们可以用复杂属性去把几个标量属性组合起来,当作一个属性使用;而当我们把这个复杂属性映射到数据库中数据列时,会把复杂属性拆开成多个标量属性。把拆成的每一个标量属性映射成一列。复杂属性作为一个整体,只会在概念层出现,在存储层没有复杂属性。因为它已经被拆成多个标量属性了。

5. Relationship 和Association:Relationship 翻译过来是“关系”(一对一,一对多,多对多共三种,表示“某一端的重数”),是一个抽象的概念,用于表示实体与实体的一种对应情况。和数据库中表与表的“关系”是一样的:Association翻译过来是“关联”或“联系”,反映到界面上就是指设计图中的一条线。表示的是一个实体可以导航到另一个实体,并不表示“重数”。只有把它们二者结合起来,才能既表示“导航”又表示“重数”。

6. *.edmx文件的结构

 在项目中的*.edmx文件上右键-》Open with... -> Xml 编辑器。点击“确定”,观察*.Edmx文件是一个Xml文件。

   该Xml文件内,首先看 <edmx:Runtime></edmx:Runtime>节点。<edmx:Runtime>节点内有三个子节点: 

  •    <edmx:StorageModels> <edmx:StorageModels>   节点是SSDL(Storage Schema Definition Language)。主要是对物理存储层上的数据库、数据表、字段的描述。内部有包含实体和关联的子节点。
  •    <edmx:ConceptualModels>     <edmx:ConceptualModels> 节点是  CSDL (Conceptual Schema Definition Language) 。主要是概念层,实体类,实体类集合,实体属性的描述。开发人员直接使用概念层定义的实体类和属性。内部有包含数据表和关联的子节点。
  •    <edmx:Mappings>  <edmx:Mappings>   节点是  C-S mapping (Conceptual-Storage-Mapping)。主要是将CSDL中定义的实体和关联映射到SSDL中的数据表和关联。

下面是EFDemo中的*.edmx的xml的形式。

Northwind.edmx
  1 <?xml version="1.0" encoding="utf-8"?>
2 <edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
3 <!-- EF Runtime content -->
4 <edmx:Runtime>
5 <!-- SSDL content -->
6 <edmx:StorageModels>
7 <Schema Namespace="NorthwindModel.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/2009/02/edm/ssdl">
8 <EntityContainer Name="NorthwindModelStoreContainer">
9 <EntitySet Name="Categories" EntityType="NorthwindModel.Store.Categories" store:Type="Tables" Schema="dbo" />
10 <EntitySet Name="Products" EntityType="NorthwindModel.Store.Products" store:Type="Tables" Schema="dbo" />
11 <AssociationSet Name="FK_Products_Categories" Association="NorthwindModel.Store.FK_Products_Categories">
12 <End Role="Categories" EntitySet="Categories" />
13 <End Role="Products" EntitySet="Products" />
14 </AssociationSet>
15 </EntityContainer>
16 <EntityType Name="Categories">
17 <Key>
18 <PropertyRef Name="CategoryID" />
19 </Key>
20 <Property Name="CategoryID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
21 <Property Name="CategoryName" Type="nvarchar" Nullable="false" MaxLength="15" />
22 <Property Name="Description" Type="ntext" />
23 <Property Name="Picture" Type="image" />
24 </EntityType>
25 <EntityType Name="Products">
26 <Key>
27 <PropertyRef Name="ProductID" />
28 </Key>
29 <Property Name="ProductID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
30 <Property Name="ProductName" Type="nvarchar" Nullable="false" MaxLength="40" />
31 <Property Name="SupplierID" Type="int" />
32 <Property Name="CategoryID" Type="int" />
33 <Property Name="QuantityPerUnit" Type="nvarchar" MaxLength="20" />
34 <Property Name="UnitPrice" Type="money" />
35 <Property Name="UnitsInStock" Type="smallint" />
36 <Property Name="UnitsOnOrder" Type="smallint" />
37 <Property Name="ReorderLevel" Type="smallint" />
38 <Property Name="Discontinued" Type="bit" Nullable="false" />
39 </EntityType>
40 <Association Name="FK_Products_Categories">
41 <End Role="Categories" Type="NorthwindModel.Store.Categories" Multiplicity="0..1" />
42 <End Role="Products" Type="NorthwindModel.Store.Products" Multiplicity="*" />
43 <ReferentialConstraint>
44 <Principal Role="Categories">
45 <PropertyRef Name="CategoryID" />
46 </Principal>
47 <Dependent Role="Products">
48 <PropertyRef Name="CategoryID" />
49 </Dependent>
50 </ReferentialConstraint>
51 </Association>
52 </Schema>
53 </edmx:StorageModels>
54 <!-- CSDL content -->
55 <edmx:ConceptualModels>
56 <Schema Namespace="NorthwindModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
57 <EntityContainer Name="NorthwindEntities" annotation:LazyLoadingEnabled="true">
58 <EntitySet Name="Categories" EntityType="NorthwindModel.Category" />
59 <EntitySet Name="Products" EntityType="NorthwindModel.Product" />
60 <AssociationSet Name="FK_Products_Categories" Association="NorthwindModel.FK_Products_Categories">
61 <End Role="Categories" EntitySet="Categories" />
62 <End Role="Products" EntitySet="Products" />
63 </AssociationSet>
64 </EntityContainer>
65 <EntityType Name="Category">
66 <Key>
67 <PropertyRef Name="CategoryID" />
68 </Key>
69 <Property Name="CategoryID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
70 <Property Name="CategoryName" Type="String" Nullable="false" MaxLength="15" Unicode="true" FixedLength="false" />
71 <Property Name="Description" Type="String" MaxLength="Max" Unicode="true" FixedLength="false" />
72 <Property Name="Picture" Type="Binary" MaxLength="Max" FixedLength="false" />
73 <NavigationProperty Name="Products" Relationship="NorthwindModel.FK_Products_Categories" FromRole="Categories" ToRole="Products" />
74 </EntityType>
75 <EntityType Name="Product">
76 <Key>
77 <PropertyRef Name="ProductID" />
78 </Key>
79 <Property Name="ProductID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
80 <Property Name="ProductName" Type="String" Nullable="false" MaxLength="40" Unicode="true" FixedLength="false" />
81 <Property Name="SupplierID" Type="Int32" />
82 <Property Name="CategoryID" Type="Int32" />
83 <Property Name="QuantityPerUnit" Type="String" MaxLength="20" Unicode="true" FixedLength="false" />
84 <Property Name="UnitPrice" Type="Decimal" Precision="19" Scale="4" />
85 <Property Name="UnitsInStock" Type="Int16" />
86 <Property Name="UnitsOnOrder" Type="Int16" />
87 <Property Name="ReorderLevel" Type="Int16" />
88 <Property Name="Discontinued" Type="Boolean" Nullable="false" />
89 <NavigationProperty Name="Category" Relationship="NorthwindModel.FK_Products_Categories" FromRole="Products" ToRole="Categories" />
90 </EntityType>
91 <Association Name="FK_Products_Categories">
92 <End Role="Categories" Type="NorthwindModel.Category" Multiplicity="0..1" />
93 <End Role="Products" Type="NorthwindModel.Product" Multiplicity="*" />
94 <ReferentialConstraint>
95 <Principal Role="Categories">
96 <PropertyRef Name="CategoryID" />
97 </Principal>
98 <Dependent Role="Products">
99 <PropertyRef Name="CategoryID" />
100 </Dependent>
101 </ReferentialConstraint>
102 </Association>
103 </Schema>
104 </edmx:ConceptualModels>
105 <!-- C-S mapping content -->
106 <edmx:Mappings>
107 <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
108 <EntityContainerMapping StorageEntityContainer="NorthwindModelStoreContainer" CdmEntityContainer="NorthwindEntities">
109 <EntitySetMapping Name="Categories"><EntityTypeMapping TypeName="NorthwindModel.Category"><MappingFragment StoreEntitySet="Categories">
110 <ScalarProperty Name="CategoryID" ColumnName="CategoryID" />
111 <ScalarProperty Name="CategoryName" ColumnName="CategoryName" />
112 <ScalarProperty Name="Description" ColumnName="Description" />
113 <ScalarProperty Name="Picture" ColumnName="Picture" />
114 </MappingFragment></EntityTypeMapping></EntitySetMapping>
115 <EntitySetMapping Name="Products">
116 <EntityTypeMapping TypeName="IsTypeOf(NorthwindModel.Product)">
117 <MappingFragment StoreEntitySet="Products">
118 <ScalarProperty Name="Discontinued" ColumnName="Discontinued" />
119 <ScalarProperty Name="ReorderLevel" ColumnName="ReorderLevel" />
120 <ScalarProperty Name="UnitsOnOrder" ColumnName="UnitsOnOrder" />
121 <ScalarProperty Name="UnitsInStock" ColumnName="UnitsInStock" />
122 <ScalarProperty Name="UnitPrice" ColumnName="UnitPrice" />
123 <ScalarProperty Name="QuantityPerUnit" ColumnName="QuantityPerUnit" />
124 <ScalarProperty Name="CategoryID" ColumnName="CategoryID" />
125 <ScalarProperty Name="SupplierID" ColumnName="SupplierID" />
126 <ScalarProperty Name="ProductName" ColumnName="ProductName" />
127 <ScalarProperty Name="ProductID" ColumnName="ProductID" />
128 </MappingFragment>
129 </EntityTypeMapping>
130 </EntitySetMapping>
131 </EntityContainerMapping>
132 </Mapping>
133 </edmx:Mappings>
134 </edmx:Runtime>
135 <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
136 <Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
137 <Connection>
138 <DesignerInfoPropertySet>
139 <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
140 </DesignerInfoPropertySet>
141 </Connection>
142 <Options>
143 <DesignerInfoPropertySet>
144 <DesignerProperty Name="ValidateOnBuild" Value="true" />
145 <DesignerProperty Name="EnablePluralization" Value="False" />
146 <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
147 </DesignerInfoPropertySet>
148 </Options>
149 <!-- Diagram content (shape and connector positions) -->
150 <Diagrams>
151 <Diagram Name="Northwind">
152 <EntityTypeShape EntityType="NorthwindModel.Category" Width="1.5" PointX="1.5" PointY="0.875" Height="1.7908333333333335" IsExpanded="true" />
153 <EntityTypeShape EntityType="NorthwindModel.Product" Width="1.5" PointX="3.75" PointY="0.625" Height="2.8025520833333331" IsExpanded="true" />
154 <AssociationConnector Association="NorthwindModel.FK_Products_Categories" ManuallyRouted="false">
155 <ConnectorPoint PointX="3" PointY="1.7704166666666668" />
156 <ConnectorPoint PointX="3.75" PointY="1.7704166666666668" />
157 </AssociationConnector>
158 </Diagram>
159 </Diagrams>
160 </Designer>
161 </edmx:Edmx>



7. EDM生成的类:当我们使用EDM向导时(不论是Model First还是Database First),设计器会利用EntityModelCodeGenerator这个工具去生成一个文件*.edmx.cs。该文件中产生相应的类及属性。

  • 容器类:默认情况下会以App.Config连接字符串名字生成一个容器类,如(NorthwindEntities)。容器类从ObjectContext继承而来。容器类含有可以返回任何一个实体的集合的属性,如Products属性将返回一个 ObjectSet<Product>集合。
  • 实体类:每一个数据表会生成一个实体类(如Product)。实体类从EntityObject继承而来。每一个实体类都有一个工厂方法,如CreateProduct(Int32 productID, String productName, Boolean discontinued),参数是在数据库中该表的非空字段所对应的属性。
  • 容器类和实体类都是Partial 类。这样你可以在不同的地方(*cs文件中)定义同一个类,在编译时不同文件中的定义部分是合并到一起,编译成一个整体类的。使用过WPF的朋友,应该对这partial 不陌生。使用partial的好处之一是在项目开发的任何一阶段可以方便地对已有类进行灵活扩展。
原文地址:https://www.cnblogs.com/marksun/p/2291930.html