模型结构
[概念模型]中要有[实体键], [实体键]要与表中的 [主键] 对应,也就是说表必须要有主键.
表中的[唯一键]不会在[概念模型]中体现
在[概念模型]中默认是不允许修改[实体键]的值的
联合主健可以正常映射
如果为属性赋值超过字段长度保存时,会向数据库提交,数据库会返回错误
联合主健的主外关系可以正常映射
只有基于主健的主外关系可以在模型向导中自动建立
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"); } } // private string _dd; [EdmScalarProperty()] [DataMember()] public string dd { get { return this._dd; } set { this.ReportPropertyChanging("dd"); this._dd = StructuralObject.SetValidValue(value, true); this.ReportPropertyChanged("dd"); } } } |
使用
myContext cn; EntityConnection econ = new EntityConnection(); string s = @" metadata=res:/myModel.ssdl |res:/myModel.csdl |res:/myModel.msl ; provider=System.Data.SqlClient; provider connection string="" Data Source=.; Initial Catalog=LingTestDB; Integrated Security=True; MultipleActiveResultSets=True; "" "; EntityConnection econ = new EntityConnection(); econ.ConnectionString = econString; EntityCommand ecmd = new EntityCommand(); ecmd.CommandType = CommandType.Text; ecmd.Connection = econ; ecmd.CommandText = "myContext.PRinsertDBItem"; ecmd.CommandType = CommandType.StoredProcedure; EntityParameter p1 = new EntityParameter("ItemID", System.Data.DbType.String); p1.Value = "aaa"; EntityParameter p2 = new EntityParameter("ItemMatter", System.Data.DbType.String); p2.Value = "bbb"; ecmd.Parameters.Add(p1); ecmd.Parameters.Add(p2); econ.Open(); ecmd.ExecuteNonQuery(); |
参数返回值型存储过程
存储过程
算加法 PRadd |
CREATE PROCEDURE PRadd @x int = 0, @y int = 0, @s int = 0 output as set @s= @x * @y |
使用向导生成映射
代码实现
string econString = @" metadata=res:/myModel.ssdl |res://*/myModel.msl ; provider=System.Data.SqlClient; provider connection string="" Data Source=.; Initial Catalog=LingTestDB; Integrated Security=True; MultipleActiveResultSets=True; "" "; EntityConnection econ = new EntityConnection(); econ.ConnectionString = econString; EntityCommand ecmd = new EntityCommand(); ecmd.CommandType = CommandType.Text; ecmd.Connection = econ; ecmd.CommandText = "myContext.PRadd"; ecmd.CommandType = CommandType.StoredProcedure; EntityParameter p1 = new EntityParameter("x", System.Data.DbType.Int32); p1.Value = 123; EntityParameter p2 = new EntityParameter("y", System.Data.DbType.Int32); p2.Value = 456; EntityParameter rt = new EntityParameter("s", System.Data.DbType.Int32, 0, ParameterDirection.Output, false, 0, 0, "", DataRowVersion.Current, 0); ecmd.Parameters.Add(p1); ecmd.Parameters.Add(p2); ecmd.Parameters.Add(rt); econ.Open(); ecmd.ExecuteNonQuery(); Console.WriteLine(rt.Value); //579 |