ADO.NET Entity Framework通过Modeel First和DataBase First,提供了几个把数据库表映射到对象上的曾。通过Database First,可以从一个数据库架构开始,使用Visual Studio 项模版创建完整映射。还可以先用设计器(Model First)设计实体类,再把它映射到数据库上,在该数据库中,表和表之间的关系可以有完全不同的结构。

需要定义的层如下:

  1、逻辑层--该层定义关系数据

  2、概念层--该层定义.NET类

  3、映射层--该层定义从.NET类到关系表和关系的映射。

(1)逻辑层--逻辑层由SSDL(存储架构定义语言定义,描述数据库表及其关系的结构。)

<edmx:StorageModels>
      <Schema Namespace="ExtjsmvcModel.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="ExtjsmvcModelStoreContainer">
          <EntitySet Name="beian" EntityType="ExtjsmvcModel.Store.beian" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="Menu" EntityType="ExtjsmvcModel.Store.Menu" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="Users" EntityType="ExtjsmvcModel.Store.Users" store:Type="Tables" Schema="dbo" />
        </EntityContainer>
        <EntityType Name="beian">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="kehu_name" Type="varchar" MaxLength="50" />
          <Property Name="beianhao" Type="varchar" MaxLength="100" />
          <Property Name="beianpass" Type="varchar" MaxLength="100" />
          <Property Name="beianemail" Type="varchar" MaxLength="100" />
          <Property Name="emailpass" Type="varchar" MaxLength="100" />
          <Property Name="beianzh" Type="varchar" MaxLength="100" />
          <Property Name="beianzhpa" Type="varchar" MaxLength="100" />
        </EntityType>
        <EntityType Name="Menu">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="Menuname" Type="varchar" MaxLength="100" />
          <Property Name="ParentID" Type="int" />
          <Property Name="cls" Type="varchar" MaxLength="100" />
        </EntityType>
        <EntityType Name="Users">
          <Key>
            <PropertyRef Name="UserId" />
          </Key>
          <Property Name="UserId" Type="int" Nullable="false" />
          <Property Name="UserName" Type="varchar" MaxLength="100" />
          <Property Name="UserPwd" Type="varchar" MaxLength="100" />
        </EntityType>
      </Schema>
    </edmx:StorageModels>

(2)概念层(定义.NET类)该层用CSDL--概念架构定义语言定义。

<edmx:ConceptualModels>
      <Schema Namespace="ExtjsmvcModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
        <EntityContainer Name="ExtjsmvcEntities" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="beian" EntityType="ExtjsmvcModel.beian" />
          <EntitySet Name="Menu" EntityType="ExtjsmvcModel.Menu" />
          <EntitySet Name="Users" EntityType="ExtjsmvcModel.Users" />
        </EntityContainer>
        <EntityType Name="beian">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="kehu_name" Type="String" MaxLength="50" Unicode="false" FixedLength="false" />
          <Property Name="beianhao" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
          <Property Name="beianpass" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
          <Property Name="beianemail" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
          <Property Name="emailpass" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
          <Property Name="beianzh" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
          <Property Name="beianzhpa" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
        </EntityType>
        <EntityType Name="Menu">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Name="Menuname" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
          <Property Name="ParentID" Type="Int32" />
          <Property Name="cls" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
        </EntityType>
        <EntityType Name="Users">
          <Key>
            <PropertyRef Name="UserId" />
          </Key>
          <Property Name="UserId" Type="Int32" Nullable="false" />
          <Property Name="UserName" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
          <Property Name="UserPwd" Type="String" MaxLength="100" Unicode="false" FixedLength="false" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>

(3)映射层(映射层使用MSL--映射规范语言 把CSDL中的实体类型定义映射到SSDL中)

<edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="ExtjsmvcModelStoreContainer" CdmEntityContainer="ExtjsmvcEntities">
          <EntitySetMapping Name="beian">
            <EntityTypeMapping TypeName="ExtjsmvcModel.beian">
              <MappingFragment StoreEntitySet="beian">
                <ScalarProperty Name="ID" ColumnName="ID" />
                <ScalarProperty Name="kehu_name" ColumnName="kehu_name" />
                <ScalarProperty Name="beianhao" ColumnName="beianhao" />
                <ScalarProperty Name="beianpass" ColumnName="beianpass" />
                <ScalarProperty Name="beianemail" ColumnName="beianemail" />
                <ScalarProperty Name="emailpass" ColumnName="emailpass" />
                <ScalarProperty Name="beianzh" ColumnName="beianzh" />
                <ScalarProperty Name="beianzhpa" ColumnName="beianzhpa" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="Menu">
            <EntityTypeMapping TypeName="ExtjsmvcModel.Menu">
              <MappingFragment StoreEntitySet="Menu">
                <ScalarProperty Name="ID" ColumnName="ID" />
                <ScalarProperty Name="Menuname" ColumnName="Menuname" />
                <ScalarProperty Name="ParentID" ColumnName="ParentID" />
                <ScalarProperty Name="cls" ColumnName="cls" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="Users">
            <EntityTypeMapping TypeName="ExtjsmvcModel.Users">
              <MappingFragment StoreEntitySet="Users">
                <ScalarProperty Name="UserId" ColumnName="UserId" />
                <ScalarProperty Name="UserName" ColumnName="UserName" />
                <ScalarProperty Name="UserPwd" ColumnName="UserPwd" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>

  用设计器和CSDL创建的实体类一般派生自基类EntityObject。

  例如Users类派生自基类EntityObject,并为其数据定义属性,如UserName。这些属性的set访问器以两种不同的方式触发信息的改变:

  一种方式是调用EntityObject基类的ReportPropertyChanging()和ReportPropertyChanged()方法。调用这些方法会使用INotifyProperty Changing和INotifyPropertyChanged接口,以通知每个客户端用这些接口的事件来注册。

  另一种方式使用用部分方法,如OnTitleChanging()和OnTitleChanged()。他们默认没有实现方式,但可以在这个类的自定义扩展中实现他们。

  

Entity Framework(一) 映射的更多相关文章

  1. asp.net mvc常用的数据注解和验证以及entity framework数据映射

    终于有时间整理一下asp.net mvc 和 entity framework 方面的素材了. 闲话少说,步入正题: 下面是model层的管理员信息表,也是大伙比较常用到的,看看下面的代码大伙应该不会 ...

  2. 将 Entity Framework、LINQ 和 Model-First 用于 Oracle 数据库

    目的 本教程介绍如何使用 Entity Framework.语言集成查询 (LINQ),以及如何对 Oracle 数据库使用 Model-First 以生成数据定义语言 (DDL) 脚本. 所需时间 ...

  3. Entity Framework中的主从表关系的使用

    其关系图: 我们使用Entity Framework生成映射关系如下: 其中author表中的books导航属性为一个集合,表示当前作者的书. 在页面中我们可以使用如下代码来实现: 代码中author ...

  4. 第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用

    上一篇 第二篇:Entity Framework CodeFirst & Model 映射 主要介绍以Fluent API来实作EntityFramework CodeFirst,得到了大家一 ...

  5. 《Entity Framework 6 Recipes》中文翻译系列 (8) -----第二章 实体数据建模基础之继承关系映射TPT

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 2-8 Table per Type Inheritance 建模 问题 你有这样一 ...

  6. 《Entity Framework 6 Recipes》中文翻译系列 (9) -----第二章 实体数据建模基础之继承关系映射TPH

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 2-10 Table per Hierarchy Inheritance 建模 问题 ...

  7. 《Entity Framework 6 Recipes》中文翻译系列 (10) -----第二章 实体数据建模基础之两实体间Is-a和Has-a关系建模、嵌入值映射

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 2-11 两实体间Is-a和Has-a关系建模 问题 你有两张有Is-a和Has-a ...

  8. 《Entity Framework 6 Recipes》中文翻译系列 (35) ------ 第六章 继承与建模高级应用之TPH继承映射中使用复合条件

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-11  TPH继承映射中使用复合条件 问题 你想使用TPH为一张表建模,建模中使 ...

  9. 《Entity Framework 6 Recipes》中文翻译系列 (36) ------ 第六章 继承与建模高级应用之TPC继承映射

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-12  TPC继承映射建模 问题 你有两张或多张架构和数据类似的表,你想使用TP ...

  10. Entity Framework Code First属性映射约定

    Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...

随机推荐

  1. Kinect For Windows V2开发日志四:使用OpenCV显示深度图像

    代码示例: #include <Kinect.h> #include <iostream> #include <opencv2\highgui.hpp> using ...

  2. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

  3. 【原】 twemproxy ketama一致性hash分析

    转贴请注明原帖位置:http://www.cnblogs.com/basecn/p/4288456.html 测试Twemproxy集群,双主双活 向twemproxy集群做写操作时,发现key的分布 ...

  4. macOS10.12允许所有来源设置

    如何调出允许所有来源呢? 很简单一行命令搞定 调出允许所有来源 1.打开终端执行命令 sudo spctl --master-disable 2.你在打开偏好设置--> 安全与隐私   好了赶快 ...

  5. 使用secureCRT连接VMware-Ubuntukylin虚拟机

    使用SecureCRT连接VMware时总是提醒主机拒绝连接.这时可以使用sudo apt-get install openssh-server openssh-client,在主机上安装ssh. 安 ...

  6. 快速调试的VS设置

    这是2013年“惹”的“祸”. 自己一直使用着VS2012,以前的调试是相当方便的,或许是之前的同事设置好的VS,我一直不会去注意我停掉调试(停掉调试的意思是:将状态1正在调试的状态,变更为状态2待启 ...

  7. asp.net实现通用水晶报表

    此片博文是在你有一定水晶报表基础的前提下参阅的:如果对于水晶报表的基础知识比较薄弱建议先去了解下水晶报表: 因为项目需要,研究了下水晶报表.说实在,这个组件很强大,但是用起来也很麻烦.刚开始使用遇到了 ...

  8. CSV文件导入到SQL Server表中

    USE 数据库名BULK Insert dbo.表名From 'c:\CSV文件名.csv'  WITH (   FIELDTERMINATOR = ',',   ROWTERMINATOR = '\ ...

  9. 如何下载免费英特尔® 实感™ SDK

    英特尔® 实感™ SDK支持的用途包括手/指跟踪.面部分析.语音识别和合成.背景分段.增强现实性等等,给你带来全新的人机交互体验.还不赶紧跟着我们一起免费下载英特尔® 实感™ SDK吧! 1. 输入网 ...

  10. 20141030--SQL2008常用命令-1

    create database biao2--创建新的数据库 go use biao2 go create table shuiguo--创建表shuiguo ,create table创建表 ( 序 ...