1、概述
当我们使用ADO.NET数据实体模型生成的时候,在项目目下生成一个.edmx文件的同时,还会在app.config里面出现如下一个代码串:
<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="Model1Container" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;此处省略一万字;"
providerName="System.Data.EntityClient" /> </connectionStrings> </configuration>
其中的csdl、ssdl、msl到是什么意思呢?
2、基本概念
2.1 CSDL
CSDL:Conceptual Schema Definition Language,概念架构定义语言。 是一种基于 XML 的语言,它描述构成数据驱动应用程序的概念模型的实体、关系和函数。即其中定义的实体、主键、属性、关联等都是对应于.NET Framework中的类型。此概念模型可以由实体框架 或 ADO.NET Data Services 使用。 实体框架使用以 CSDL 描述的元数据将在概念模型中定义的实体和关系映射到数据源。
下列是生成的.edmx文件中关于csdl部分的示例:
<!-- CSDL content --> <edmx:ConceptualModels> <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self"
xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"> <EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true"> <EntitySet Name="t_crobot_adwords" EntityType="Model1.t_crobot_adwords" /> </EntityContainer> <EntityType Name="t_crobot_adwords"> <Key> <PropertyRef Name="id" /> </Key> <Property Type="Int64" Name="id" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Type="Int64" Name="curr_eims_id" /> <Property Type="Int64" Name="eims_id" /> <Property Type="Int64" Name="user_id" /> <Property Type="Int64" Name="site_id" /> <Property Type="Int64" Name="release_site_id" /> <Property Type="Decimal" Name="using_money" /> <Property Type="Int64" Name="count" /> <Property Type="Int64" Name="send_count" /> <Property Type="Int32" Name="week_count" /> <Property Type="DateTime" Name="week_time" /> <Property Type="Int32" Name="pick_count" /> <Property Type="Int16" Name="state" /> <Property Type="String" Name="name" MaxLength="255" FixedLength="false" Unicode="false" /> <Property Type="String" Name="domain" MaxLength="255" FixedLength="false" Unicode="false" /> <Property Type="String" Name="adwords" MaxLength="255" FixedLength="false" Unicode="false" /> <Property Type="Int64" Name="trade_id" /> <Property Type="String" Name="trade_name" MaxLength="50" FixedLength="false" Unicode="false" /> <Property Type="Int16" Name="site_type" /> <Property Type="String" Name="adviser" MaxLength="50" FixedLength="false" Unicode="false" /> <Property Type="DateTime" Name="pass_time" /> <Property Type="DateTime" Name="enable_time" /> <Property Type="DateTime" Name="create_time" /> <Property Type="String" Name="company_name" MaxLength="255" FixedLength="false" Unicode="false" /> <Property Type="String" Name="xny_db_pwd" MaxLength="255" FixedLength="false" Unicode="false" /> <Property Type="Int64" Name="link_id" /> <Property Type="Int64" Name="task_count" /> <Property Type="Int64" Name="included_count" /> <Property Type="Int64" Name="remark_id" /> </EntityType> </Schema> </edmx:ConceptualModels>
2.2 SSDL
SSDL:Store Schema Definition Language,存储架构定义语言。是一种基于 XML 的语言,用于描述实体框架应用程序的存储模型。即表、列、关系、主键及索引等数据库中存在的概念。在实体框架应用程序中,存储模型元数据从 .ssdl 文件(用 SSDL 编写)加载到 System.Data.Metadata.Edm.StoreItemCollection 的实例中,并且可以使用 System.Data.Metadata.Edm.MetadataWorkspace 类中的方法进行访问。 实体框架使用存储模型元数据将针对概念模型的查询转换为特定于存储的命令。
下列是生成的.edmx文件中关于SSDL的示例:
<!-- SSDL content --> <edmx:StorageModels> <Schema Namespace="Model1.Store" Alias="Self" Provider="MySql.Data.MySqlClient" ProviderManifestToken="5.6"
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"> <EntityContainer Name="Model1StoreContainer"> <EntitySet Name="t_crobot_adwords" EntityType="Model1.Store.t_crobot_adwords" store:Type="Tables" Schema="testbak" /> <EntitySet Name="v_crobot_article_modules" EntityType="Model1.Store.v_crobot_article_modules" store:Type="Views"
store:Schema="testbak" store:Name="v_crobot_article_modules"> <DefiningQuery>SELECT `v_crobot_article_modules`.`sourceId`, `v_crobot_article_modules`.`moduleId`, `v_crobot_article_modules`.`sourceName`, `v_crobot_article_modules`.`moduleName`, `v_crobot_article_modules`.`lastCount`, `v_crobot_article_modules`.`totalCount`, `v_crobot_article_modules`.`createTime`, `v_crobot_article_modules`.`lastTime`, `v_crobot_article_modules`.`tradeId` FROM `v_crobot_article_modules` AS `v_crobot_article_modules`
</DefiningQuery> </EntitySet> </EntityContainer> <EntityType Name="t_crobot_adwords"> <Key> <PropertyRef Name="id" /> </Key> <Property Name="id" Type="bigint" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="curr_eims_id" Type="bigint" /> <Property Name="eims_id" Type="bigint" /> <Property Name="user_id" Type="bigint" /> <Property Name="site_id" Type="bigint" /> <Property Name="release_site_id" Type="bigint" /> <Property Name="using_money" Type="decimal" /> <Property Name="count" Type="bigint" /> <Property Name="send_count" Type="bigint" /> <Property Name="week_count" Type="int" /> <Property Name="week_time" Type="datetime" /> <Property Name="pick_count" Type="int" /> <Property Name="state" Type="smallint" /> <Property Name="name" Type="varchar" MaxLength="255" /> <Property Name="domain" Type="varchar" MaxLength="255" /> <Property Name="adwords" Type="varchar" MaxLength="255" /> <Property Name="trade_id" Type="bigint" /> <Property Name="trade_name" Type="varchar" MaxLength="50" /> <Property Name="site_type" Type="smallint" /> <Property Name="adviser" Type="varchar" MaxLength="50" /> <Property Name="pass_time" Type="datetime" /> <Property Name="enable_time" Type="datetime" /> <Property Name="create_time" Type="datetime" /> <Property Name="company_name" Type="varchar" MaxLength="255" /> <Property Name="xny_db_pwd" Type="varchar" MaxLength="255" /> <Property Name="link_id" Type="bigint" /> <Property Name="task_count" Type="bigint" /> <Property Name="included_count" Type="bigint" /> <Property Name="remark_id" Type="bigint" /> </EntityType> <!--生成过程中发现错误: 警告 6002: 表/视图“def.testbak.v_crobot_article_modules”未定义主键。已推断出该键,并将定义创建为只读的表/视图。 --> <EntityType Name="v_crobot_article_modules"> <Key> <PropertyRef Name="sourceId" /> <PropertyRef Name="moduleId" /> </Key> <Property Name="sourceId" Type="int" Nullable="false" /> <Property Name="moduleId" Type="int" Nullable="false" /> <Property Name="sourceName" Type="varchar" MaxLength="100" /> <Property Name="moduleName" Type="varchar" MaxLength="100" /> <Property Name="lastCount" Type="int" /> <Property Name="totalCount" Type="int" /> <Property Name="createTime" Type="datetime" /> <Property Name="lastTime" Type="datetime" /> <Property Name="tradeId" Type="int" /> </EntityType> </Schema></edmx:StorageModels>
2.3 MSL
MSL:Mapping Specification Language,映射规范语言。是一种基于 XML 的语言,它描述了实体框架应用程序的概念模型与存储模型之间的映射。在实体框架应用程序中,映射元数据在生成时是从 .msl 文件(以 MSL 编写)加载的。 实体框架在运行时使用映射元数据将对概念模型的查询转换为存储特定的命令。
下列是生成.edmx文件中关于MSL的部分示例:
<!-- C-S mapping content --> <edmx:Mappings> <Mapping xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs" Space="C-S"> <Alias Key="Model" Value="Model1" /> <Alias Key="Target" Value="Model1.Store" /> <EntityContainerMapping CdmEntityContainer="Model1Container" StorageEntityContainer="Model1StoreContainer"> <EntitySetMapping Name="t_crobot_adwords"> <EntityTypeMapping TypeName="Model1.t_crobot_adwords"> <MappingFragment StoreEntitySet="t_crobot_adwords"> <ScalarProperty Name="remark_id" ColumnName="remark_id" /> <ScalarProperty Name="included_count" ColumnName="included_count" /> <ScalarProperty Name="task_count" ColumnName="task_count" /> <ScalarProperty Name="link_id" ColumnName="link_id" /> <ScalarProperty Name="xny_db_pwd" ColumnName="xny_db_pwd" /> <ScalarProperty Name="company_name" ColumnName="company_name" /> <ScalarProperty Name="create_time" ColumnName="create_time" /> <ScalarProperty Name="enable_time" ColumnName="enable_time" /> <ScalarProperty Name="pass_time" ColumnName="pass_time" /> <ScalarProperty Name="adviser" ColumnName="adviser" /> <ScalarProperty Name="site_type" ColumnName="site_type" /> <ScalarProperty Name="trade_name" ColumnName="trade_name" /> <ScalarProperty Name="trade_id" ColumnName="trade_id" /> <ScalarProperty Name="adwords" ColumnName="adwords" /> <ScalarProperty Name="domain" ColumnName="domain" /> <ScalarProperty Name="name" ColumnName="name" /> <ScalarProperty Name="state" ColumnName="state" /> <ScalarProperty Name="pick_count" ColumnName="pick_count" /> <ScalarProperty Name="week_time" ColumnName="week_time" /> <ScalarProperty Name="week_count" ColumnName="week_count" /> <ScalarProperty Name="send_count" ColumnName="send_count" /> <ScalarProperty Name="count" ColumnName="count" /> <ScalarProperty Name="using_money" ColumnName="using_money" /> <ScalarProperty Name="release_site_id" ColumnName="release_site_id" /> <ScalarProperty Name="site_id" ColumnName="site_id" /> <ScalarProperty Name="user_id" ColumnName="user_id" /> <ScalarProperty Name="eims_id" ColumnName="eims_id" /> <ScalarProperty Name="curr_eims_id" ColumnName="curr_eims_id" /> <ScalarProperty Name="id" ColumnName="id" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> </EntityContainerMapping> </Mapping> </edmx:Mappings>
3、小结
CSDL、MSL与SSDL组合在一起就是EDMX模式。EDMX模式在项目中的表现形式就是扩展名为edmx的文件,该文件本质也是xml文件,可手动进行编辑。该文件就相当于DLL,我们在使用EF的时候,主要是还用这个文件。