现在的位置: 首页 > 综合 > 正文

ADO.NET Entity Framework(2)建模

2011年01月10日 ⁄ 综合 ⁄ 共 5996字 ⁄ 字号 评论关闭

目录

1    模型结构    1

1.1    模型关系说明    1

1.2    模型设计器结构说明    2

2    EDM    3

2.1    Model设计器    3

2.2    edmx文件    4

2.3    Context    5

2.4    实体类    5

2.5    使用    7

3    使用向导创建模型    7

4    映射基本规则    9

5    映射条件    11

6    继承    12

7    多表联合    13

8    关联    14

8.1    [(1)—(*)] 关联    14

8.1.1    表    14

8.1.2    设置    14

8.1.3    操作数据    15

8.2    [(1)—(1)]关联    17

8.2.1    表    17

8.2.2    设置    17

8.2.3    操作数据    18

8.3    [(1)—(0..1)]关联    19

8.4    [(*)—(*)]关联    19

8.4.1    表    19

8.4.2    显示中转方式    19

8.4.3    隐示中转方式    20

8.4.4    操作数据    20

9    映射存储过程与函数    22

9.1    返回表型存储过程    22

9.1.1    存储过程    22

9.1.2    使用向导生成映射    22

9.1.3    代码实现    23

9.2    命令型存储过程    23

9.2.1    存储过程    23

9.2.2    使用向导生成映射    24

9.2.3    代码实现    24

9.3    参数返回值型存储过程    25

9.3.1    存储过程    25

9.3.2    使用向导生成映射    25

9.3.3    代码实现    26

 

 

模型结构

[概念模型]中要有[实体键], [实体键]要与表中的 [主键] 对应,也就是说表必须要有主键.

 

表中的[唯一键]不会在[概念模型]中体现

 

在[概念模型]中默认是不允许修改[实体键]的值的

 

联合主健可以正常映射

 

如果为属性赋值超过字段长度保存时,会向数据库提交,数据库会返回错误

 

联合主健的主外关系可以正常映射

 

只有基于主健的主外关系可以在模型向导中自动建立

 

Conceptual Model

概念模型

用于描述实体(Entity)类型及其关系

Storage Model

存储模型

用于描述数据库实际存储架构

Mapping Specification

映射规范

将概念模型和存储模型连接起来,以便进行操作转换

Entity Class

实体类

用于描述实体的属性,每一个实体类都会定义一个属性或多个属性为一个键属性(Key Properties),用于唯一标识一个实体

实体类型可以通过继承关系加以扩展

Entity Set

实体集

实体(Entity)存在于实体集(Entity Set)中,就像表格中的行存在于表格中的一样

Entity Containe

实体容器,

实体集定义在实体容器(Entity Container)中

关联

关联

定义了实体之间的关系,可以直接通过关联,来访问相关联的对象,关联分为一对一、一对多、多对多

关联通过Association Type来定义,过实体类中的Navigation属性就可以访问与实体相关联的实体

 

 

 

模型关系说明

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

模型设计器结构说明

 

 

 

 

 

 

 

EDM

EF 没有采取 LINQ to SQL 基于Attribute映射的做法。

为了适应变化和提供更多的数据库类型扩展,EF 提供了专门的定义语言来完成模型设置

Conceptual schema definition language (.csdl)

 

Store schema definition language (.ssdl)

 

Mapping specification language (.msl)

 

 

默认,Model设计器将(.csdl)(.ssdl)(.msl)存放在一个名为(.edmx)的XML 格式定义文件,并会根据设计自动生成对应的Context与实体类

 

Model设计器

数据库中的表

模型

模型生成选项

 

edmx文件

 

<?xml
version="1.0"
encoding="utf-8"?>

<edmx:Edmx
Version="1.0"
xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">

<!-- EF Runtime content -->

 

<edmx:Runtime>

存储模型

<!-- SSDL content -->

<edmx:StorageModels>

<Schema
Namespace="myModel.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">

<EntityContainer
Name="myModelStoreContainer">

<EntitySet
Name="myTab"
EntityType="myModel.Store.myTab"
store:Type="Tables"
Schema="dbo" />

</EntityContainer>

<EntityType
Name="myTab">

<Key>

<PropertyRef
Name="a" />

</Key>

<Property
Name="a"
Type="nchar"
Nullable="false"
MaxLength="10" />

<Property
Name="b"
Type="nchar"
Nullable="false"
MaxLength="10" />

<Property
Name="c"
Type="nchar"
MaxLength="10" />

<Property
Name="d"
Type="nchar"
MaxLength="10" />

</EntityType>

</Schema>

</edmx:StorageModels>

概念模型

<!-- CSDL content -->

<edmx:ConceptualModels>

<Schema
Namespace="myModel"
Alias="Self"
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">

<EntityContainer
Name="mySets">

<EntitySet
Name="myTab"
EntityType="myModel.myTab" />

</EntityContainer>

<EntityType
Name="myTab">

<Key>

<PropertyRef
Name="aa" />

</Key>

<Property
Name="aa"
Type="String"
Nullable="false"
MaxLength="10"
Unicode="true"
FixedLength="true" />

<Property
Name="bb"
Type="String"
Nullable="false"
MaxLength="10"
Unicode="true"
FixedLength="true" />

<Property
Name="cc"
Type="String"
MaxLength="10"
Unicode="true"
FixedLength="true" />

<Property
Name="dd"
Type="String"
MaxLength="10"
Unicode="true"
FixedLength="true" />

 

</EntityType>

</Schema>

</edmx:ConceptualModels>

映射

<!-- C-S mapping content -->

<edmx:Mappings>

<Mapping
Space="C-S"
xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">

<EntityContainerMapping
StorageEntityContainer="myModelStoreContainer"
CdmEntityContainer="mySets">

<EntitySetMapping
Name="myTab">

<EntityTypeMapping
TypeName="IsTypeOf(myModel.myTab)">

<MappingFragment
StoreEntitySet="myTab">

<ScalarProperty
Name="aa"
ColumnName="a" />

<ScalarProperty
Name="bb"
ColumnName="b" />

<ScalarProperty
Name="cc"
ColumnName="d" />

<ScalarProperty
Name="dd"
ColumnName="c" />

</MappingFragment>

</EntityTypeMapping>

</EntitySetMapping>

</EntityContainerMapping>

</Mapping>

</edmx:Mappings>

 

</edmx:Runtime>

图形设计器的配置部份

<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->

<edmx:Designer
xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">

<edmx:Connection>

<DesignerInfoPropertySet>

<DesignerProperty
Name="MetadataArtifactProcessing"
Value="EmbedInOutputAssembly" />

</DesignerInfoPropertySet>

</edmx:Connection>

<edmx:Options>

<DesignerInfoPropertySet>

<DesignerProperty
Name="ValidateOnBuild"
Value="true" />

</DesignerInfoPropertySet>

</edmx:Options>

<!-- Diagram content (shape and connector positions) -->

<edmx:Diagrams>

<Diagram
Name="myModel">

<EntityTypeShape
EntityType="myModel.myTab"
Width="1.5"
PointX="0.75"
PointY="0.75"
Height="1.7"
IsExpanded="true" />

</Diagram>

</edmx:Diagrams>

</edmx:Designer>

 

</edmx:Edmx>

 

Context


public
class
myContext :ObjectContext

{

 


public myContext(EntityConnection connection) :


base(connection, "mySets")

{

 

}

 


public
ObjectQuery<myTab> myTab

{


get

{


if ((this._myTab == null))

{


this._myTab = base.CreateQuery<myTab>("[myTab]");

}


return
this._myTab;

}

}


private
ObjectQuery<myTab> _myTab;

 

 


public
void AddTomyTab(myTab myTab)

{


base.AddObject("myTab", myTab);

}

}

 

实体类

[EdmEntityType(NamespaceName = "myModel", Name = "myTab")]

[DataContract(IsReference = true)]

[Serializable()]

 


public
class
myTab :EntityObject

{

 


public
static
myTab CreatemyTab(string aa, string bb)

{


myTab myTab = new
myTab();

myTab.aa = aa;

myTab.bb = bb;


return myTab;

}

 


private
string _aa;

[EdmScalarProperty(EntityKeyProperty = true, IsNullable = false)]

[DataMember()]


public
string aa

{


get

{


return
this._aa;

}


set

{


this.ReportPropertyChanging("aa");


this._aa = StructuralObject.SetValidValue(value, false);


this.ReportPropertyChanged("aa");

}

}

 

 

 


private
string _bb;

[EdmScalarPropertyAttribute(IsNullable = false)]

[DataMemberAttribute()]


public
string bb

{


get

{


return
this._bb;

}


set

{

 


this.ReportPropertyChanging("bb");


this._bb = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false);


this.ReportPropertyChanged("bb");

 

}

}

 

 


//


private
string _cc;

[EdmScalarProperty()]

[DataMember()]


public
string cc

{


get

{


return
this._cc;

}


set

{


this.ReportPropertyChanging("cc");


this._cc =StructuralObject.SetValidValue(value, true);


this.ReportPropertyChanged("cc");

}

}


抱歉!评论已关闭.