本篇学习Entity Framework的DataBase First开发模式。
Entity Framework 支持用于在概念级别定义数据的Entity Data Model,使用ADO.NET Entity Data Model Designer(ADO.NET 实体数据模型设计器,与Visual Studio 组建集成在一起)时,概念模型、存储模型和映射信息包含在.edmx文件中。
使用实体数据模型设计器
第一步首先创建数据库,此处直接使用Northwind 数据库
第二步创建一个控制台应用程序 EntityFrameworkSample
第三步应用程序右键添加新建项,添加ADO.NET 实体数据模型
确定后 弹出新窗口
选择默认,下一步,配置连接字符串
完成后生成模型
Northwind.edmx文件包含了概念模型、存储模型和映射信息。edmx文件的本质是一个xml文件
CSDL部分
<edmx:ConceptualModels> <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"> <EntityContainer Name="NorthwindEntities" annotation:LazyLoadingEnabled="true"> <EntitySet Name="Customers" EntityType="NorthwindModel.Customer" /> <EntitySet Name="Order_Details" EntityType="NorthwindModel.Order_Detail" /> <EntitySet Name="Orders" EntityType="NorthwindModel.Order" /> <AssociationSet Name="FK_Orders_Customers" Association="NorthwindModel.FK_Orders_Customers"> <End Role="Customer" EntitySet="Customers" /> <End Role="Order" EntitySet="Orders" /> </AssociationSet> <AssociationSet Name="FK_Order_Details_Orders" Association="NorthwindModel.FK_Order_Details_Orders"> <End Role="Order" EntitySet="Orders" /> <End Role="Order_Detail" EntitySet="Order_Details" /> </AssociationSet> </EntityContainer> <EntityType Name="Customer"> <Key> <PropertyRef Name="CustomerID" /> </Key> <Property Type="String" Name="CustomerID" Nullable="false" MaxLength="5" FixedLength="true" Unicode="true" /> <Property Type="String" Name="CompanyName" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true" /> <Property Type="String" Name="ContactName" MaxLength="30" FixedLength="false" Unicode="true" /> <Property Type="String" Name="ContactTitle" MaxLength="30" FixedLength="false" Unicode="true" /> <Property Type="String" Name="Address" MaxLength="60" FixedLength="false" Unicode="true" /> <Property Type="String" Name="City" MaxLength="15" FixedLength="false" Unicode="true" /> <Property Type="String" Name="Region" MaxLength="15" FixedLength="false" Unicode="true" /> <Property Type="String" Name="PostalCode" MaxLength="10" FixedLength="false" Unicode="true" /> <Property Type="String" Name="Country" MaxLength="15" FixedLength="false" Unicode="true" /> <Property Type="String" Name="Phone" MaxLength="24" FixedLength="false" Unicode="true" /> <Property Type="String" Name="Fax" MaxLength="24" FixedLength="false" Unicode="true" /> <NavigationProperty Name="Orders" Relationship="NorthwindModel.FK_Orders_Customers" FromRole="Customer" ToRole="Order" /> </EntityType> <EntityType Name="Order_Detail"> <Key> <PropertyRef Name="OrderID" /> <PropertyRef Name="ProductID" /> </Key> <Property Type="Int32" Name="OrderID" Nullable="false" /> <Property Type="Int32" Name="ProductID" Nullable="false" /> <Property Type="Decimal" Name="UnitPrice" Nullable="false" Precision="19" Scale="4" /> <Property Type="Int16" Name="Quantity" Nullable="false" /> <Property Type="Single" Name="Discount" Nullable="false" /> <NavigationProperty Name="Order" Relationship="NorthwindModel.FK_Order_Details_Orders" FromRole="Order_Detail" ToRole="Order" /> </EntityType> <EntityType Name="Order"> <Key> <PropertyRef Name="OrderID" /> </Key> <Property Type="Int32" Name="OrderID" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Type="String" Name="CustomerID" MaxLength="5" FixedLength="true" Unicode="true" /> <Property Type="Int32" Name="EmployeeID" /> <Property Type="DateTime" Name="OrderDate" /> <Property Type="DateTime" Name="RequiredDate" /> <Property Type="DateTime" Name="ShippedDate" /> <Property Type="Int32" Name="ShipVia" /> <Property Type="Decimal" Name="Freight" Precision="19" Scale="4" /> <Property Type="String" Name="ShipName" MaxLength="40" FixedLength="false" Unicode="true" /> <Property Type="String" Name="ShipAddress" MaxLength="60" FixedLength="false" Unicode="true" /> <Property Type="String" Name="ShipCity" MaxLength="15" FixedLength="false" Unicode="true" /> <Property Type="String" Name="ShipRegion" MaxLength="15" FixedLength="false" Unicode="true" /> <Property Type="String" Name="ShipPostalCode" MaxLength="10" FixedLength="false" Unicode="true" /> <Property Type="String" Name="ShipCountry" MaxLength="15" FixedLength="false" Unicode="true" /> <NavigationProperty Name="Customer" Relationship="NorthwindModel.FK_Orders_Customers" FromRole="Order" ToRole="Customer" /> <NavigationProperty Name="Order_Details" Relationship="NorthwindModel.FK_Order_Details_Orders" FromRole="Order" ToRole="Order_Detail" /> </EntityType> <Association Name="FK_Orders_Customers"> <End Type="NorthwindModel.Customer" Role="Customer" Multiplicity="0..1" /> <End Type="NorthwindModel.Order" Role="Order" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Customer"> <PropertyRef Name="CustomerID" /> </Principal> <Dependent Role="Order"> <PropertyRef Name="CustomerID" /> </Dependent> </ReferentialConstraint> </Association> <Association Name="FK_Order_Details_Orders"> <End Type="NorthwindModel.Order" Role="Order" Multiplicity="1" /> <End Type="NorthwindModel.Order_Detail" Role="Order_Detail" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Order"> <PropertyRef Name="OrderID" /> </Principal> <Dependent Role="Order_Detail"> <PropertyRef Name="OrderID" /> </Dependent> </ReferentialConstraint> </Association> </Schema> </edmx:ConceptualModels>
EntityContainer
是一种用于容纳实体集、关系集和函数倒入的逻辑容器。概念模型实体容器通过 EntityContainerMapping元素映射到存储模型实体容器。存储模型实体容器描述的数据库的结构:实体集描述表、关联集描述外键约束、函数导入描述数据库中的存储过程 。
EntityContainer元素可以具有零个或多个下列子元素(按所列顺序)
EntitySet
AssociationSet
FunctionImport
批注元素
<EntityContainer Name="NorthwindEntities" annotation:LazyLoadingEnabled="true"> <EntitySet Name="Customers" EntityType="NorthwindModel.Customer" /> <EntitySet Name="Order_Details" EntityType="NorthwindModel.Order_Detail" /> <EntitySet Name="Orders" EntityType="NorthwindModel.Order" /> <AssociationSet Name="FK_Orders_Customers" Association="NorthwindModel.FK_Orders_Customers"> <End Role="Customer" EntitySet="Customers" /> <End Role="Order" EntitySet="Orders" /> </AssociationSet> <AssociationSet Name="FK_Order_Details_Orders" Association="NorthwindModel.FK_Order_Details_Orders"> <End Role="Order" EntitySet="Orders" /> <End Role="Order_Detail" EntitySet="Order_Details" /> </AssociationSet> </EntityContainer>
上面的例子是一个EntityContainer元素,该元素定义了3个实体集和2个关联集
EntitySet元素
用于某个实体类型的实例和自该实体类型派生的任何类型的实例的逻辑容器。
特性名称 | 是否必需 | 值 |
---|---|---|
Name |
是 |
实体集的名称。 |
EntityType |
是 |
实体集包含其实例的实体类型的完全限定名称。 |
AssociationSet元素
用于相同类型的关联实例的逻辑容器。
AssociationSet元素可以具有以下子元素(按所列顺序)
Documentation(0个或1个元素)
End(正好2个元素)
Annotation元素(0个或多个元素)
Association特性制定关联集包含的关联的类型。应使用正好两个 End 子元素指定构成关联集两端的实体集。
EntityType元素
表示概念模型中的顶级概念的结构。实体类型是应用程序中实体类型实例的模板。 每个模板都包含以下信息:
- 唯一名称。 (必需)。
- 由一个或多个属性定义的实体键。 (必需)。
- 用于包含数据的属性。 (可选)。
- 导航属性,用于从关联的一端导航至另一端。 (可选)。
EntityType 元素可以具有以下子元素(按所列顺序):
- Documentation(零个或一个元素)
- Key(零或一个元素)
- Property(零个或多个元素)
- NavigationProperty(零个或多个元素)
- 批注元素(零个或多个元素)
SSDL部分
描述了表、列、关系、主键及索引等数据库中存在的概念。
<edmx:StorageModels> <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"> <EntityContainer Name="NorthwindModelStoreContainer"> <EntitySet Name="Customers" EntityType="NorthwindModel.Store.Customers" store:Type="Tables" Schema="dbo" /> <EntitySet Name="Order Details" EntityType="NorthwindModel.Store.Order Details" store:Type="Tables" Schema="dbo" /> <EntitySet Name="Orders" EntityType="NorthwindModel.Store.Orders" store:Type="Tables" Schema="dbo" /> <AssociationSet Name="FK_Order_Details_Orders" Association="NorthwindModel.Store.FK_Order_Details_Orders"> <End Role="Orders" EntitySet="Orders" /> <End Role="Order Details" EntitySet="Order Details" /> </AssociationSet> <AssociationSet Name="FK_Orders_Customers" Association="NorthwindModel.Store.FK_Orders_Customers"> <End Role="Customers" EntitySet="Customers" /> <End Role="Orders" EntitySet="Orders" /> </AssociationSet> </EntityContainer> <EntityType Name="Customers"> <Key> <PropertyRef Name="CustomerID" /> </Key> <Property Name="CustomerID" Type="nchar" Nullable="false" MaxLength="5" /> <Property Name="CompanyName" Type="nvarchar" Nullable="false" MaxLength="40" /> <Property Name="ContactName" Type="nvarchar" MaxLength="30" /> <Property Name="ContactTitle" Type="nvarchar" MaxLength="30" /> <Property Name="Address" Type="nvarchar" MaxLength="60" /> <Property Name="City" Type="nvarchar" MaxLength="15" /> <Property Name="Region" Type="nvarchar" MaxLength="15" /> <Property Name="PostalCode" Type="nvarchar" MaxLength="10" /> <Property Name="Country" Type="nvarchar" MaxLength="15" /> <Property Name="Phone" Type="nvarchar" MaxLength="24" /> <Property Name="Fax" Type="nvarchar" MaxLength="24" /> </EntityType> <EntityType Name="Order Details"> <Key> <PropertyRef Name="OrderID" /> <PropertyRef Name="ProductID" /> </Key> <Property Name="OrderID" Type="int" Nullable="false" /> <Property Name="ProductID" Type="int" Nullable="false" /> <Property Name="UnitPrice" Type="money" Nullable="false" /> <Property Name="Quantity" Type="smallint" Nullable="false" /> <Property Name="Discount" Type="real" Nullable="false" /> </EntityType> <EntityType Name="Orders"> <Key> <PropertyRef Name="OrderID" /> </Key> <Property Name="OrderID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="CustomerID" Type="nchar" MaxLength="5" /> <Property Name="EmployeeID" Type="int" /> <Property Name="OrderDate" Type="datetime" /> <Property Name="RequiredDate" Type="datetime" /> <Property Name="ShippedDate" Type="datetime" /> <Property Name="ShipVia" Type="int" /> <Property Name="Freight" Type="money" /> <Property Name="ShipName" Type="nvarchar" MaxLength="40" /> <Property Name="ShipAddress" Type="nvarchar" MaxLength="60" /> <Property Name="ShipCity" Type="nvarchar" MaxLength="15" /> <Property Name="ShipRegion" Type="nvarchar" MaxLength="15" /> <Property Name="ShipPostalCode" Type="nvarchar" MaxLength="10" /> <Property Name="ShipCountry" Type="nvarchar" MaxLength="15" /> </EntityType> <Association Name="FK_Order_Details_Orders"> <End Role="Orders" Type="NorthwindModel.Store.Orders" Multiplicity="1" /> <End Role="Order Details" Type="NorthwindModel.Store.Order Details" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Orders"> <PropertyRef Name="OrderID" /> </Principal> <Dependent Role="Order Details"> <PropertyRef Name="OrderID" /> </Dependent> </ReferentialConstraint> </Association> <Association Name="FK_Orders_Customers"> <End Role="Customers" Type="NorthwindModel.Store.Customers" Multiplicity="0..1" /> <End Role="Orders" Type="NorthwindModel.Store.Orders" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Customers"> <PropertyRef Name="CustomerID" /> </Principal> <Dependent Role="Orders"> <PropertyRef Name="CustomerID" /> </Dependent> </ReferentialConstraint> </Association> </Schema></edmx:StorageModels>
文档结果和csdl中的很相像。只是其中的EntityType等使用数据库中的概念的描述。
MSL部分
映射规范语言 (MSL) 是一种基于XML的语言,它描述了实体框架 应用程序的概念模型与存储模型之间的映射。
在实体框架应用程序中,映射元数据是在生成时从 .msl 文件(以 MSL 编写)加载的。实体框架在运行时使用映射元数据将对概念模型的查询转换为存储特定的命令。
<edmx:Mappings> <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs"> <EntityContainerMapping StorageEntityContainer="NorthwindModelStoreContainer" CdmEntityContainer="NorthwindEntities"> <EntitySetMapping Name="Customers"> <EntityTypeMapping TypeName="NorthwindModel.Customer"> <MappingFragment StoreEntitySet="Customers"> <ScalarProperty Name="Fax" ColumnName="Fax" /> <ScalarProperty Name="Phone" ColumnName="Phone" /> <ScalarProperty Name="Country" ColumnName="Country" /> <ScalarProperty Name="PostalCode" ColumnName="PostalCode" /> <ScalarProperty Name="Region" ColumnName="Region" /> <ScalarProperty Name="City" ColumnName="City" /> <ScalarProperty Name="Address" ColumnName="Address" /> <ScalarProperty Name="ContactTitle" ColumnName="ContactTitle" /> <ScalarProperty Name="ContactName" ColumnName="ContactName" /> <ScalarProperty Name="CompanyName" ColumnName="CompanyName" /> <ScalarProperty Name="CustomerID" ColumnName="CustomerID" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <EntitySetMapping Name="Order_Details"> <EntityTypeMapping TypeName="NorthwindModel.Order_Detail"> <MappingFragment StoreEntitySet="Order Details"> <ScalarProperty Name="Discount" ColumnName="Discount" /> <ScalarProperty Name="Quantity" ColumnName="Quantity" /> <ScalarProperty Name="UnitPrice" ColumnName="UnitPrice" /> <ScalarProperty Name="ProductID" ColumnName="ProductID" /> <ScalarProperty Name="OrderID" ColumnName="OrderID" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <EntitySetMapping Name="Orders"> <EntityTypeMapping TypeName="NorthwindModel.Order"> <MappingFragment StoreEntitySet="Orders"> <ScalarProperty Name="ShipCountry" ColumnName="ShipCountry" /> <ScalarProperty Name="ShipPostalCode" ColumnName="ShipPostalCode" /> <ScalarProperty Name="ShipRegion" ColumnName="ShipRegion" /> <ScalarProperty Name="ShipCity" ColumnName="ShipCity" /> <ScalarProperty Name="ShipAddress" ColumnName="ShipAddress" /> <ScalarProperty Name="ShipName" ColumnName="ShipName" /> <ScalarProperty Name="Freight" ColumnName="Freight" /> <ScalarProperty Name="ShipVia" ColumnName="ShipVia" /> <ScalarProperty Name="ShippedDate" ColumnName="ShippedDate" /> <ScalarProperty Name="RequiredDate" ColumnName="RequiredDate" /> <ScalarProperty Name="OrderDate" ColumnName="OrderDate" /> <ScalarProperty Name="EmployeeID" ColumnName="EmployeeID" /> <ScalarProperty Name="CustomerID" ColumnName="CustomerID" /> <ScalarProperty Name="OrderID" ColumnName="OrderID" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> </EntityContainerMapping> </Mapping> </edmx:Mappings>
EntityContainerMapping元素的StorageEntityContainer(值为SSDL的EntityContainer元素的name值) 和 CdmEntityContainer(值为CSDL的EntityContainer元素的name值)
EntitySetMapping元素
以映射规范语言 (MSL) 表示的 EntitySetMapping 元素将概念模型实体集中所有类型映射到存储模型中的实体集
EntitySetMapping 元素可以具有以下子元素:
- EntityTypeMapping(零个或多个)
- QueryView(零个或一个)
- MappingFragment(零个或多个)