前面提到过这个网址:性能注意事项(实体框架) https://msdn.microsoft.com/zh-cn/library/cc853327.aspx
注意版本: .NET Framework (current version) (还有一个其他版本, .NET Framework 4).
此文提到 查询执行的各个阶段[格式:操作(相对成本)]:
加载元数据(中等),打开数据库连接(中等),生成视图(高),准备查询(中等),执行查询(低),加载和验证类型(低),跟踪(低),是对象具体化(中等)
还是截个图吧.
其中,我特别在意‘生成视图‘这个阶段,高成本(其它阶段要么频率低要么成本低).
生成视图,注释如下:在实体框架可以针对概念模型执行查询或将更改保存到数据源之前,它必须生成一组本地查询视图才能访问数据库。 由于生成这些视图会产生很高的成本,因此,您可以在设计时预生成视图并将它们添加到项目。 有关详细信息,请参阅How to: Pre-Generate Views to Improve Query Performance。
于是,我就去看How to: Pre-Generate Views to Improve Query Performance(以下简称H文)。(如何:预生成视图以提高查询性能)
动手,建立解决方案名EFTest,控制台名EFIce.
简要说下H文的步骤(跟H文有差异):
a.打开PhoneBookModel.edmx,在模型浏览器里,找到PhoneBookModel,右键,属性,有一项属性名为‘元数据项目处理‘,默认值是‘嵌入输出程序集中‘,更改为‘复制到输出目录‘;
b.生成PhoneBookModel.ssdl,PhoneBookModel.csdl,PhoneBookModel.msl,PhoneBookModel.Views.cs四个文件并加入到项目里,其中.ssdl,.csdl,.msl三个文件的属性生成操作设置为‘嵌入的资源‘,而PhoneBookModel.Views.cs文件会直接编译到.dll里.
此处生成文件的方法:命令提示符执行
C:\Users\Administrator>EdmGen /mode:FullGeneration /connectionstring:"server=.;user id=sa;password=******;persist security info=True;database=PhoneBook" /provider:"System.Data.SqlClient" /outssdl:PhoneBookModel.ssdl /outcsdl:PhoneBookModel.csdl /outmsl:PhoneBookModel.msl /outobjectlayer:PhoneBookObject.cs /outviews:PhoneBook.Views.cs /namespace:"EFIce" /entitycontainer:"PhoneBookEntitiesContainer
产生的文件的位置C:\Users\Administrator目录下.
PhoneBookModel.edmx的图解:
文件结果:
//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.36282 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ [assembly: System.Data.Mapping.EntityViewGenerationAttribute(typeof(Edm_EntityMappingGeneratedViews.ViewsForBaseEntitySets9E94E67D0053D8FDE07EEEB8F52E0E47D0A29AB22FB0BCB5EB6BE8BC009D5748))] namespace Edm_EntityMappingGeneratedViews { /// <Summary> /// 此类型包含在设计时生成的 EntitySets 和 AssociationSets 的视图。 /// </Summary> public sealed class ViewsForBaseEntitySets9E94E67D0053D8FDE07EEEB8F52E0E47D0A29AB22FB0BCB5EB6BE8BC009D5748 : System.Data.Mapping.EntityViewContainer { /// <Summary> /// 构造函数存储各区的视图,以及根据元数据和映射结束和视图生成的哈希值。 /// </Summary> public ViewsForBaseEntitySets9E94E67D0053D8FDE07EEEB8F52E0E47D0A29AB22FB0BCB5EB6BE8BC009D5748() { this.EdmEntityContainerName = "PhoneBookEntitiesContainer"; this.StoreEntityContainerName = "EFIceStoreContainer"; this.HashOverMappingClosure = "4c6b0bf3e3884e8b4a6c4c7c60d99b787ec990e9b24297519ab203951cadc07d"; this.HashOverAllExtentViews = "d048405239ad31569ab8a2a9f67381c39d4130d40b625903b35e1e8d2d1266f5"; this.ViewCount = 8; } /// <Summary> /// 此方法返回给定索引的视图。 /// </Summary> protected override System.Collections.Generic.KeyValuePair<string, string> GetViewAt(int index) { if ((index == 0)) { return GetView0(); } if ((index == 1)) { return GetView1(); } if ((index == 2)) { return GetView2(); } if ((index == 3)) { return GetView3(); } if ((index == 4)) { return GetView4(); } if ((index == 5)) { return GetView5(); } if ((index == 6)) { return GetView6(); } if ((index == 7)) { return GetView7(); } throw new System.IndexOutOfRangeException(); } /// <Summary> /// 返回 EFIceStoreContainer.AccountInfo 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView0() { return new System.Collections.Generic.KeyValuePair<string, string>("EFIceStoreContainer.AccountInfo", @" SELECT VALUE -- Constructing AccountInfo [EFIce.Store.AccountInfo](T1.AccountInfo_ID, T1.AccountInfo_Account, T1.AccountInfo_AccountName, T1.AccountInfo_Pwd) FROM ( SELECT T.ID AS AccountInfo_ID, T.Account AS AccountInfo_Account, T.AccountName AS AccountInfo_AccountName, T.Pwd AS AccountInfo_Pwd, True AS _from0 FROM PhoneBookEntitiesContainer.AccountInfo AS T ) AS T1"); } /// <Summary> /// 返回 PhoneBookEntitiesContainer.AccountInfo 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView1() { return new System.Collections.Generic.KeyValuePair<string, string>("PhoneBookEntitiesContainer.AccountInfo", @" SELECT VALUE -- Constructing AccountInfo [EFIce.AccountInfo](T1.AccountInfo_ID, T1.AccountInfo_Account, T1.AccountInfo_AccountName, T1.AccountInfo_Pwd) FROM ( SELECT T.ID AS AccountInfo_ID, T.Account AS AccountInfo_Account, T.AccountName AS AccountInfo_AccountName, T.Pwd AS AccountInfo_Pwd, True AS _from0 FROM EFIceStoreContainer.AccountInfo AS T ) AS T1"); } /// <Summary> /// 返回 EFIceStoreContainer.ContactInfo 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView2() { return new System.Collections.Generic.KeyValuePair<string, string>("EFIceStoreContainer.ContactInfo", @" SELECT VALUE -- Constructing ContactInfo [EFIce.Store.ContactInfo](T1.ContactInfo_ID, T1.ContactInfo_ContactId, T1.ContactInfo_IsDelete, T1.ContactInfo_Account, T1.ContactInfo_ContactName, T1.ContactInfo_CommonMobile, T1.ContactInfo_HeadPortrait, T1.ContactInfo_AttFile, T1.ContactInfo_GroupId) FROM ( SELECT T.ID AS ContactInfo_ID, T.ContactId AS ContactInfo_ContactId, T.IsDelete AS ContactInfo_IsDelete, T.Account AS ContactInfo_Account, T.ContactName AS ContactInfo_ContactName, T.CommonMobile AS ContactInfo_CommonMobile, T.HeadPortrait AS ContactInfo_HeadPortrait, T.AttFile AS ContactInfo_AttFile, T.GroupId AS ContactInfo_GroupId, True AS _from0 FROM PhoneBookEntitiesContainer.ContactInfo AS T ) AS T1"); } /// <Summary> /// 返回 EFIceStoreContainer.GroupInfo 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView3() { return new System.Collections.Generic.KeyValuePair<string, string>("EFIceStoreContainer.GroupInfo", @" SELECT VALUE -- Constructing GroupInfo [EFIce.Store.GroupInfo](T1.GroupInfo_GroupId, T1.GroupInfo_GroupName) FROM ( SELECT T.GroupId AS GroupInfo_GroupId, T.GroupName AS GroupInfo_GroupName, True AS _from0 FROM PhoneBookEntitiesContainer.GroupInfo AS T ) AS T1"); } /// <Summary> /// 返回 PhoneBookEntitiesContainer.ContactInfo 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView4() { return new System.Collections.Generic.KeyValuePair<string, string>("PhoneBookEntitiesContainer.ContactInfo", @" SELECT VALUE -- Constructing ContactInfo [EFIce.ContactInfo](T1.ContactInfo_ID, T1.ContactInfo_ContactId, T1.ContactInfo_IsDelete, T1.ContactInfo_Account, T1.ContactInfo_ContactName, T1.ContactInfo_CommonMobile, T1.ContactInfo_HeadPortrait, T1.ContactInfo_AttFile, T1.ContactInfo_GroupId) FROM ( SELECT T.ID AS ContactInfo_ID, T.ContactId AS ContactInfo_ContactId, T.IsDelete AS ContactInfo_IsDelete, T.Account AS ContactInfo_Account, T.ContactName AS ContactInfo_ContactName, T.CommonMobile AS ContactInfo_CommonMobile, T.HeadPortrait AS ContactInfo_HeadPortrait, T.AttFile AS ContactInfo_AttFile, T.GroupId AS ContactInfo_GroupId, True AS _from0 FROM EFIceStoreContainer.ContactInfo AS T ) AS T1"); } /// <Summary> /// 返回 PhoneBookEntitiesContainer.GroupInfo 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView5() { return new System.Collections.Generic.KeyValuePair<string, string>("PhoneBookEntitiesContainer.GroupInfo", @" SELECT VALUE -- Constructing GroupInfo [EFIce.GroupInfo](T1.GroupInfo_GroupId, T1.GroupInfo_GroupName) FROM ( SELECT T.GroupId AS GroupInfo_GroupId, T.GroupName AS GroupInfo_GroupName, True AS _from0 FROM EFIceStoreContainer.GroupInfo AS T ) AS T1"); } /// <Summary> /// 返回 EFIceStoreContainer.ContactInGroup 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView6() { return new System.Collections.Generic.KeyValuePair<string, string>("EFIceStoreContainer.ContactInGroup", @" SELECT VALUE -- Constructing ContactInGroup [EFIce.Store.ContactInGroup](T1.ContactInGroup_Id, T1.ContactInGroup_Account, T1.ContactInGroup_GroupNumber, T1.ContactInGroup_ContactId, T1.ContactInGroup_IsDelete, T1.ContactInGroup_UpdateTime) FROM ( SELECT T.Id AS ContactInGroup_Id, T.Account AS ContactInGroup_Account, T.GroupNumber AS ContactInGroup_GroupNumber, T.ContactId AS ContactInGroup_ContactId, T.IsDelete AS ContactInGroup_IsDelete, T.UpdateTime AS ContactInGroup_UpdateTime, True AS _from0 FROM PhoneBookEntitiesContainer.ContactInGroup AS T ) AS T1"); } /// <Summary> /// 返回 PhoneBookEntitiesContainer.ContactInGroup 的视图 /// </Summary> private System.Collections.Generic.KeyValuePair<string, string> GetView7() { return new System.Collections.Generic.KeyValuePair<string, string>("PhoneBookEntitiesContainer.ContactInGroup", @" SELECT VALUE -- Constructing ContactInGroup [EFIce.ContactInGroup](T1.ContactInGroup_Id, T1.ContactInGroup_Account, T1.ContactInGroup_GroupNumber, T1.ContactInGroup_ContactId, T1.ContactInGroup_IsDelete, T1.ContactInGroup_UpdateTime) FROM ( SELECT T.Id AS ContactInGroup_Id, T.Account AS ContactInGroup_Account, T.GroupNumber AS ContactInGroup_GroupNumber, T.ContactId AS ContactInGroup_ContactId, T.IsDelete AS ContactInGroup_IsDelete, T.UpdateTime AS ContactInGroup_UpdateTime, True AS _from0 FROM EFIceStoreContainer.ContactInGroup AS T ) AS T1"); } } }
PhoneBook.Views.cs
<?xml version="1.0" encoding="utf-8"?> <Schema Namespace="EFIce" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> <EntityContainer Name="PhoneBookEntitiesContainer" annotation:LazyLoadingEnabled="true"> <EntitySet Name="AccountInfo" EntityType="EFIce.AccountInfo" /> <EntitySet Name="ContactInfo" EntityType="EFIce.ContactInfo" /> <EntitySet Name="ContactInGroup" EntityType="EFIce.ContactInGroup" /> <EntitySet Name="GroupInfo" EntityType="EFIce.GroupInfo" /> <AssociationSet Name="FK_ContactInfo_GroupInfo" Association="EFIce.FK_ContactInfo_GroupInfo"> <End Role="GroupInfo" EntitySet="GroupInfo" /> <End Role="ContactInfo" EntitySet="ContactInfo" /> </AssociationSet> </EntityContainer> <EntityType Name="AccountInfo"> <Key> <PropertyRef Name="ID" /> </Key> <Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Name="Account" Type="String" MaxLength="64" Unicode="true" FixedLength="false" /> <Property Name="AccountName" Type="String" MaxLength="50" Unicode="true" FixedLength="false" /> <Property Name="Pwd" Type="String" MaxLength="50" Unicode="true" FixedLength="false" /> </EntityType> <EntityType Name="ContactInfo"> <Key> <PropertyRef Name="ID" /> </Key> <Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Name="ContactId" Type="String" Nullable="false" MaxLength="128" Unicode="true" FixedLength="false" /> <Property Name="IsDelete" Type="Int32" Nullable="false" /> <Property Name="Account" Type="String" Nullable="false" MaxLength="64" Unicode="true" FixedLength="false" /> <Property Name="ContactName" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" /> <Property Name="CommonMobile" Type="String" MaxLength="50" Unicode="true" FixedLength="false" /> <Property Name="HeadPortrait" Type="String" MaxLength="256" Unicode="true" FixedLength="false" /> <Property Name="AttFile" Type="String" MaxLength="256" Unicode="true" FixedLength="false" /> <Property Name="GroupId" Type="Int32" /> <NavigationProperty Name="GroupInfo" Relationship="EFIce.FK_ContactInfo_GroupInfo" FromRole="ContactInfo" ToRole="GroupInfo" /> </EntityType> <EntityType Name="ContactInGroup"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="String" Nullable="false" MaxLength="128" Unicode="false" FixedLength="false" /> <Property Name="Account" Type="String" Nullable="false" MaxLength="64" Unicode="true" FixedLength="false" /> <Property Name="GroupNumber" Type="String" Nullable="false" MaxLength="16" Unicode="true" FixedLength="false" /> <Property Name="ContactId" Type="String" Nullable="false" MaxLength="128" Unicode="false" FixedLength="false" /> <Property Name="IsDelete" Type="Int32" /> <Property Name="UpdateTime" Type="DateTime" /> </EntityType> <EntityType Name="GroupInfo"> <Key> <PropertyRef Name="GroupId" /> </Key> <Property Name="GroupId" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Name="GroupName" Type="String" Nullable="false" MaxLength="300" Unicode="true" FixedLength="false" /> <NavigationProperty Name="ContactInfo" Relationship="EFIce.FK_ContactInfo_GroupInfo" FromRole="GroupInfo" ToRole="ContactInfo" /> </EntityType> <Association Name="FK_ContactInfo_GroupInfo"> <End Role="GroupInfo" Type="EFIce.GroupInfo" Multiplicity="0..1" /> <End Role="ContactInfo" Type="EFIce.ContactInfo" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="GroupInfo"> <PropertyRef Name="GroupId" /> </Principal> <Dependent Role="ContactInfo"> <PropertyRef Name="GroupId" /> </Dependent> </ReferentialConstraint> </Association> </Schema>
PhoneBookModel.csdl
<?xml version="1.0" encoding="utf-8"?> <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs"> <EntityContainerMapping StorageEntityContainer="EFIceStoreContainer" CdmEntityContainer="PhoneBookEntitiesContainer"> <EntitySetMapping Name="AccountInfo"> <EntityTypeMapping TypeName="EFIce.AccountInfo"> <MappingFragment StoreEntitySet="AccountInfo"> <ScalarProperty Name="ID" ColumnName="ID" /> <ScalarProperty Name="Account" ColumnName="Account" /> <ScalarProperty Name="AccountName" ColumnName="AccountName" /> <ScalarProperty Name="Pwd" ColumnName="Pwd" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <EntitySetMapping Name="ContactInfo"> <EntityTypeMapping TypeName="EFIce.ContactInfo"> <MappingFragment StoreEntitySet="ContactInfo"> <ScalarProperty Name="ID" ColumnName="ID" /> <ScalarProperty Name="ContactId" ColumnName="ContactId" /> <ScalarProperty Name="IsDelete" ColumnName="IsDelete" /> <ScalarProperty Name="Account" ColumnName="Account" /> <ScalarProperty Name="ContactName" ColumnName="ContactName" /> <ScalarProperty Name="CommonMobile" ColumnName="CommonMobile" /> <ScalarProperty Name="HeadPortrait" ColumnName="HeadPortrait" /> <ScalarProperty Name="AttFile" ColumnName="AttFile" /> <ScalarProperty Name="GroupId" ColumnName="GroupId" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <EntitySetMapping Name="ContactInGroup"> <EntityTypeMapping TypeName="EFIce.ContactInGroup"> <MappingFragment StoreEntitySet="ContactInGroup"> <ScalarProperty Name="Id" ColumnName="Id" /> <ScalarProperty Name="Account" ColumnName="Account" /> <ScalarProperty Name="GroupNumber" ColumnName="GroupNumber" /> <ScalarProperty Name="ContactId" ColumnName="ContactId" /> <ScalarProperty Name="IsDelete" ColumnName="IsDelete" /> <ScalarProperty Name="UpdateTime" ColumnName="UpdateTime" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <EntitySetMapping Name="GroupInfo"> <EntityTypeMapping TypeName="EFIce.GroupInfo"> <MappingFragment StoreEntitySet="GroupInfo"> <ScalarProperty Name="GroupId" ColumnName="GroupId" /> <ScalarProperty Name="GroupName" ColumnName="GroupName" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> </EntityContainerMapping> </Mapping>
PhoneBookModel.msl
<?xml version="1.0" encoding="utf-8"?> <Schema Namespace="EFIce.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="EFIceStoreContainer"> <EntitySet Name="AccountInfo" EntityType="EFIce.Store.AccountInfo" store:Type="Tables" Schema="dbo" /> <EntitySet Name="ContactInfo" EntityType="EFIce.Store.ContactInfo" store:Type="Tables" Schema="dbo" /> <EntitySet Name="ContactInGroup" EntityType="EFIce.Store.ContactInGroup" store:Type="Tables" Schema="dbo" /> <EntitySet Name="GroupInfo" EntityType="EFIce.Store.GroupInfo" store:Type="Tables" Schema="dbo" /> <AssociationSet Name="FK_ContactInfo_GroupInfo" Association="EFIce.Store.FK_ContactInfo_GroupInfo"> <End Role="GroupInfo" EntitySet="GroupInfo" /> <End Role="ContactInfo" EntitySet="ContactInfo" /> </AssociationSet> </EntityContainer> <EntityType Name="AccountInfo"> <Key> <PropertyRef Name="ID" /> </Key> <Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="Account" Type="nvarchar" MaxLength="64" /> <Property Name="AccountName" Type="nvarchar" MaxLength="50" /> <Property Name="Pwd" Type="nvarchar" MaxLength="50" /> </EntityType> <EntityType Name="ContactInfo"> <Key> <PropertyRef Name="ID" /> </Key> <Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="ContactId" Type="nvarchar" Nullable="false" MaxLength="128" /> <Property Name="IsDelete" Type="int" Nullable="false" /> <Property Name="Account" Type="nvarchar" Nullable="false" MaxLength="64" /> <Property Name="ContactName" Type="nvarchar" Nullable="false" MaxLength="50" /> <Property Name="CommonMobile" Type="nvarchar" MaxLength="50" /> <Property Name="HeadPortrait" Type="nvarchar" MaxLength="256" /> <Property Name="AttFile" Type="nvarchar" MaxLength="256" /> <Property Name="GroupId" Type="int" /> </EntityType> <EntityType Name="ContactInGroup"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="varchar" Nullable="false" MaxLength="128" /> <Property Name="Account" Type="nvarchar" Nullable="false" MaxLength="64" /> <Property Name="GroupNumber" Type="nvarchar" Nullable="false" MaxLength="16" /> <Property Name="ContactId" Type="varchar" Nullable="false" MaxLength="128" /> <Property Name="IsDelete" Type="int" /> <Property Name="UpdateTime" Type="datetime" /> </EntityType> <EntityType Name="GroupInfo"> <Key> <PropertyRef Name="GroupId" /> </Key> <Property Name="GroupId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="GroupName" Type="nvarchar" Nullable="false" MaxLength="300" /> </EntityType> <Association Name="FK_ContactInfo_GroupInfo"> <End Role="GroupInfo" Type="EFIce.Store.GroupInfo" Multiplicity="0..1" /> <End Role="ContactInfo" Type="EFIce.Store.ContactInfo" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="GroupInfo"> <PropertyRef Name="GroupId" /> </Principal> <Dependent Role="ContactInfo"> <PropertyRef Name="GroupId" /> </Dependent> </ReferentialConstraint> </Association> </Schema>
PhoneBookModel.ssdl
//------------------------------------------------------------------------------ // <auto-generated> // 此代码是根据模板生成的。 // // 手动更改此文件可能会导致应用程序中发生异常行为。 // 如果重新生成代码,则将覆盖对此文件的手动更改。 // </auto-generated> //------------------------------------------------------------------------------ using System; using System.ComponentModel; using System.Data.EntityClient; using System.Data.Objects; using System.Data.Objects.DataClasses; using System.Linq; using System.Runtime.Serialization; using System.Xml.Serialization; [assembly: EdmSchemaAttribute()] #region EDM 关系源元数据 [assembly: EdmRelationshipAttribute("EFIce", "FK_ContactInfo_GroupInfo", "GroupInfo", System.Data.Metadata.Edm.RelationshipMultiplicity.ZeroOrOne, typeof(EFIce.GroupInfo), "ContactInfo", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(EFIce.ContactInfo), true)] #endregion namespace EFIce { #region 上下文 /// <summary> /// 没有元数据文档可用。 /// </summary> public partial class PhoneBookEntitiesContainer : ObjectContext { #region 构造函数 /// <summary> /// 请使用应用程序配置文件的“PhoneBookEntitiesContainer”部分中的连接字符串初始化新 PhoneBookEntitiesContainer 对象。 /// </summary> public PhoneBookEntitiesContainer() : base("name=PhoneBookEntitiesContainer", "PhoneBookEntitiesContainer") { this.ContextOptions.LazyLoadingEnabled = true; OnContextCreated(); } /// <summary> /// 初始化新的 PhoneBookEntitiesContainer 对象。 /// </summary> public PhoneBookEntitiesContainer(string connectionString) : base(connectionString, "PhoneBookEntitiesContainer") { this.ContextOptions.LazyLoadingEnabled = true; OnContextCreated(); } /// <summary> /// 初始化新的 PhoneBookEntitiesContainer 对象。 /// </summary> public PhoneBookEntitiesContainer(EntityConnection connection) : base(connection, "PhoneBookEntitiesContainer") { this.ContextOptions.LazyLoadingEnabled = true; OnContextCreated(); } #endregion #region 分部方法 partial void OnContextCreated(); #endregion #region ObjectSet 属性 /// <summary> /// 没有元数据文档可用。 /// </summary> public ObjectSet<AccountInfo> AccountInfo { get { if ((_AccountInfo == null)) { _AccountInfo = base.CreateObjectSet<AccountInfo>("AccountInfo"); } return _AccountInfo; } } private ObjectSet<AccountInfo> _AccountInfo; /// <summary> /// 没有元数据文档可用。 /// </summary> public ObjectSet<ContactInfo> ContactInfo { get { if ((_ContactInfo == null)) { _ContactInfo = base.CreateObjectSet<ContactInfo>("ContactInfo"); } return _ContactInfo; } } private ObjectSet<ContactInfo> _ContactInfo; /// <summary> /// 没有元数据文档可用。 /// </summary> public ObjectSet<ContactInGroup> ContactInGroup { get { if ((_ContactInGroup == null)) { _ContactInGroup = base.CreateObjectSet<ContactInGroup>("ContactInGroup"); } return _ContactInGroup; } } private ObjectSet<ContactInGroup> _ContactInGroup; /// <summary> /// 没有元数据文档可用。 /// </summary> public ObjectSet<GroupInfo> GroupInfo { get { if ((_GroupInfo == null)) { _GroupInfo = base.CreateObjectSet<GroupInfo>("GroupInfo"); } return _GroupInfo; } } private ObjectSet<GroupInfo> _GroupInfo; #endregion #region AddTo 方法 /// <summary> /// 用于向 AccountInfo EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。 /// </summary> public void AddToAccountInfo(AccountInfo accountInfo) { base.AddObject("AccountInfo", accountInfo); } /// <summary> /// 用于向 ContactInfo EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。 /// </summary> public void AddToContactInfo(ContactInfo contactInfo) { base.AddObject("ContactInfo", contactInfo); } /// <summary> /// 用于向 ContactInGroup EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。 /// </summary> public void AddToContactInGroup(ContactInGroup contactInGroup) { base.AddObject("ContactInGroup", contactInGroup); } /// <summary> /// 用于向 GroupInfo EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。 /// </summary> public void AddToGroupInfo(GroupInfo groupInfo) { base.AddObject("GroupInfo", groupInfo); } #endregion } #endregion #region 实体 /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmEntityTypeAttribute(NamespaceName="EFIce", Name="AccountInfo")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class AccountInfo : EntityObject { #region 工厂方法 /// <summary> /// 创建新的 AccountInfo 对象。 /// </summary> /// <param name="id">ID 属性的初始值。</param> public static AccountInfo CreateAccountInfo(global::System.Int32 id) { AccountInfo accountInfo = new AccountInfo(); accountInfo.ID = id; return accountInfo; } #endregion #region 基元属性 /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 ID { get { return _ID; } set { if (_ID != value) { OnIDChanging(value); ReportPropertyChanging("ID"); _ID = StructuralObject.SetValidValue(value); ReportPropertyChanged("ID"); OnIDChanged(); } } } private global::System.Int32 _ID; partial void OnIDChanging(global::System.Int32 value); partial void OnIDChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public global::System.String Account { get { return _Account; } set { OnAccountChanging(value); ReportPropertyChanging("Account"); _Account = StructuralObject.SetValidValue(value, true); ReportPropertyChanged("Account"); OnAccountChanged(); } } private global::System.String _Account; partial void OnAccountChanging(global::System.String value); partial void OnAccountChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public global::System.String AccountName { get { return _AccountName; } set { OnAccountNameChanging(value); ReportPropertyChanging("AccountName"); _AccountName = StructuralObject.SetValidValue(value, true); ReportPropertyChanged("AccountName"); OnAccountNameChanged(); } } private global::System.String _AccountName; partial void OnAccountNameChanging(global::System.String value); partial void OnAccountNameChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public global::System.String Pwd { get { return _Pwd; } set { OnPwdChanging(value); ReportPropertyChanging("Pwd"); _Pwd = StructuralObject.SetValidValue(value, true); ReportPropertyChanged("Pwd"); OnPwdChanged(); } } private global::System.String _Pwd; partial void OnPwdChanging(global::System.String value); partial void OnPwdChanged(); #endregion } /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmEntityTypeAttribute(NamespaceName="EFIce", Name="ContactInfo")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class ContactInfo : EntityObject { #region 工厂方法 /// <summary> /// 创建新的 ContactInfo 对象。 /// </summary> /// <param name="id">ID 属性的初始值。</param> /// <param name="contactId">ContactId 属性的初始值。</param> /// <param name="isDelete">IsDelete 属性的初始值。</param> /// <param name="account">Account 属性的初始值。</param> /// <param name="contactName">ContactName 属性的初始值。</param> public static ContactInfo CreateContactInfo(global::System.Int32 id, global::System.String contactId, global::System.Int32 isDelete, global::System.String account, global::System.String contactName) { ContactInfo contactInfo = new ContactInfo(); contactInfo.ID = id; contactInfo.ContactId = contactId; contactInfo.IsDelete = isDelete; contactInfo.Account = account; contactInfo.ContactName = contactName; return contactInfo; } #endregion #region 基元属性 /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 ID { get { return _ID; } set { if (_ID != value) { OnIDChanging(value); ReportPropertyChanging("ID"); _ID = StructuralObject.SetValidValue(value); ReportPropertyChanged("ID"); OnIDChanged(); } } } private global::System.Int32 _ID; partial void OnIDChanging(global::System.Int32 value); partial void OnIDChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String ContactId { get { return _ContactId; } set { OnContactIdChanging(value); ReportPropertyChanging("ContactId"); _ContactId = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("ContactId"); OnContactIdChanged(); } } private global::System.String _ContactId; partial void OnContactIdChanging(global::System.String value); partial void OnContactIdChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 IsDelete { get { return _IsDelete; } set { OnIsDeleteChanging(value); ReportPropertyChanging("IsDelete"); _IsDelete = StructuralObject.SetValidValue(value); ReportPropertyChanged("IsDelete"); OnIsDeleteChanged(); } } private global::System.Int32 _IsDelete; partial void OnIsDeleteChanging(global::System.Int32 value); partial void OnIsDeleteChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String Account { get { return _Account; } set { OnAccountChanging(value); ReportPropertyChanging("Account"); _Account = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("Account"); OnAccountChanged(); } } private global::System.String _Account; partial void OnAccountChanging(global::System.String value); partial void OnAccountChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String ContactName { get { return _ContactName; } set { OnContactNameChanging(value); ReportPropertyChanging("ContactName"); _ContactName = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("ContactName"); OnContactNameChanged(); } } private global::System.String _ContactName; partial void OnContactNameChanging(global::System.String value); partial void OnContactNameChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public global::System.String CommonMobile { get { return _CommonMobile; } set { OnCommonMobileChanging(value); ReportPropertyChanging("CommonMobile"); _CommonMobile = StructuralObject.SetValidValue(value, true); ReportPropertyChanged("CommonMobile"); OnCommonMobileChanged(); } } private global::System.String _CommonMobile; partial void OnCommonMobileChanging(global::System.String value); partial void OnCommonMobileChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public global::System.String HeadPortrait { get { return _HeadPortrait; } set { OnHeadPortraitChanging(value); ReportPropertyChanging("HeadPortrait"); _HeadPortrait = StructuralObject.SetValidValue(value, true); ReportPropertyChanged("HeadPortrait"); OnHeadPortraitChanged(); } } private global::System.String _HeadPortrait; partial void OnHeadPortraitChanging(global::System.String value); partial void OnHeadPortraitChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public global::System.String AttFile { get { return _AttFile; } set { OnAttFileChanging(value); ReportPropertyChanging("AttFile"); _AttFile = StructuralObject.SetValidValue(value, true); ReportPropertyChanged("AttFile"); OnAttFileChanged(); } } private global::System.String _AttFile; partial void OnAttFileChanging(global::System.String value); partial void OnAttFileChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public Nullable<global::System.Int32> GroupId { get { return _GroupId; } set { OnGroupIdChanging(value); ReportPropertyChanging("GroupId"); _GroupId = StructuralObject.SetValidValue(value); ReportPropertyChanged("GroupId"); OnGroupIdChanged(); } } private Nullable<global::System.Int32> _GroupId; partial void OnGroupIdChanging(Nullable<global::System.Int32> value); partial void OnGroupIdChanged(); #endregion #region 导航属性 /// <summary> /// 没有元数据文档可用。 /// </summary> [XmlIgnoreAttribute()] [SoapIgnoreAttribute()] [DataMemberAttribute()] [EdmRelationshipNavigationPropertyAttribute("EFIce", "FK_ContactInfo_GroupInfo", "GroupInfo")] public GroupInfo GroupInfo { get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<GroupInfo>("EFIce.FK_ContactInfo_GroupInfo", "GroupInfo").Value; } set { ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<GroupInfo>("EFIce.FK_ContactInfo_GroupInfo", "GroupInfo").Value = value; } } /// <summary> /// 没有元数据文档可用。 /// </summary> [BrowsableAttribute(false)] [DataMemberAttribute()] public EntityReference<GroupInfo> GroupInfoReference { get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<GroupInfo>("EFIce.FK_ContactInfo_GroupInfo", "GroupInfo"); } set { if ((value != null)) { ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<GroupInfo>("EFIce.FK_ContactInfo_GroupInfo", "GroupInfo", value); } } } #endregion } /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmEntityTypeAttribute(NamespaceName="EFIce", Name="ContactInGroup")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class ContactInGroup : EntityObject { #region 工厂方法 /// <summary> /// 创建新的 ContactInGroup 对象。 /// </summary> /// <param name="id">Id 属性的初始值。</param> /// <param name="account">Account 属性的初始值。</param> /// <param name="groupNumber">GroupNumber 属性的初始值。</param> /// <param name="contactId">ContactId 属性的初始值。</param> public static ContactInGroup CreateContactInGroup(global::System.String id, global::System.String account, global::System.String groupNumber, global::System.String contactId) { ContactInGroup contactInGroup = new ContactInGroup(); contactInGroup.Id = id; contactInGroup.Account = account; contactInGroup.GroupNumber = groupNumber; contactInGroup.ContactId = contactId; return contactInGroup; } #endregion #region 基元属性 /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] [DataMemberAttribute()] public global::System.String Id { get { return _Id; } set { if (_Id != value) { OnIdChanging(value); ReportPropertyChanging("Id"); _Id = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("Id"); OnIdChanged(); } } } private global::System.String _Id; partial void OnIdChanging(global::System.String value); partial void OnIdChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String Account { get { return _Account; } set { OnAccountChanging(value); ReportPropertyChanging("Account"); _Account = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("Account"); OnAccountChanged(); } } private global::System.String _Account; partial void OnAccountChanging(global::System.String value); partial void OnAccountChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String GroupNumber { get { return _GroupNumber; } set { OnGroupNumberChanging(value); ReportPropertyChanging("GroupNumber"); _GroupNumber = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("GroupNumber"); OnGroupNumberChanged(); } } private global::System.String _GroupNumber; partial void OnGroupNumberChanging(global::System.String value); partial void OnGroupNumberChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String ContactId { get { return _ContactId; } set { OnContactIdChanging(value); ReportPropertyChanging("ContactId"); _ContactId = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("ContactId"); OnContactIdChanged(); } } private global::System.String _ContactId; partial void OnContactIdChanging(global::System.String value); partial void OnContactIdChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public Nullable<global::System.Int32> IsDelete { get { return _IsDelete; } set { OnIsDeleteChanging(value); ReportPropertyChanging("IsDelete"); _IsDelete = StructuralObject.SetValidValue(value); ReportPropertyChanged("IsDelete"); OnIsDeleteChanged(); } } private Nullable<global::System.Int32> _IsDelete; partial void OnIsDeleteChanging(Nullable<global::System.Int32> value); partial void OnIsDeleteChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public Nullable<global::System.DateTime> UpdateTime { get { return _UpdateTime; } set { OnUpdateTimeChanging(value); ReportPropertyChanging("UpdateTime"); _UpdateTime = StructuralObject.SetValidValue(value); ReportPropertyChanged("UpdateTime"); OnUpdateTimeChanged(); } } private Nullable<global::System.DateTime> _UpdateTime; partial void OnUpdateTimeChanging(Nullable<global::System.DateTime> value); partial void OnUpdateTimeChanged(); #endregion } /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmEntityTypeAttribute(NamespaceName="EFIce", Name="GroupInfo")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class GroupInfo : EntityObject { #region 工厂方法 /// <summary> /// 创建新的 GroupInfo 对象。 /// </summary> /// <param name="groupId">GroupId 属性的初始值。</param> /// <param name="groupName">GroupName 属性的初始值。</param> public static GroupInfo CreateGroupInfo(global::System.Int32 groupId, global::System.String groupName) { GroupInfo groupInfo = new GroupInfo(); groupInfo.GroupId = groupId; groupInfo.GroupName = groupName; return groupInfo; } #endregion #region 基元属性 /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 GroupId { get { return _GroupId; } set { if (_GroupId != value) { OnGroupIdChanging(value); ReportPropertyChanging("GroupId"); _GroupId = StructuralObject.SetValidValue(value); ReportPropertyChanged("GroupId"); OnGroupIdChanged(); } } } private global::System.Int32 _GroupId; partial void OnGroupIdChanging(global::System.Int32 value); partial void OnGroupIdChanged(); /// <summary> /// 没有元数据文档可用。 /// </summary> [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String GroupName { get { return _GroupName; } set { OnGroupNameChanging(value); ReportPropertyChanging("GroupName"); _GroupName = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("GroupName"); OnGroupNameChanged(); } } private global::System.String _GroupName; partial void OnGroupNameChanging(global::System.String value); partial void OnGroupNameChanged(); #endregion #region 导航属性 /// <summary> /// 没有元数据文档可用。 /// </summary> [XmlIgnoreAttribute()] [SoapIgnoreAttribute()] [DataMemberAttribute()] [EdmRelationshipNavigationPropertyAttribute("EFIce", "FK_ContactInfo_GroupInfo", "ContactInfo")] public EntityCollection<ContactInfo> ContactInfo { get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<ContactInfo>("EFIce.FK_ContactInfo_GroupInfo", "ContactInfo"); } set { if ((value != null)) { ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<ContactInfo>("EFIce.FK_ContactInfo_GroupInfo", "ContactInfo", value); } } } #endregion } #endregion }
PhoneBookObject.cs
c.最后就是设置App.config
测试,用不用预生成视图的结果几乎无任何差别.
我怀疑 预生成视图 没起效果.我删了新引入项目里的.ssdl,.csdl,.msl,.Views.cs文件,项目一样跑起来了.可是,我按文档(.net 4.0 版本的文档)走的啊,没有错.
我在MSDN没找到预生成视图 .net 4.5版本的资料.也许,.net 4.5之前,需要通过手动操作才能提高性能,在.net 4.5及之后已经优化了,使用高版本vs不需要额外设置,所以呢,.net 4.5搭配EF6的性能比之前.net 4的性能要好不少吧?
...也怀疑是因为用的控制台而不是asp.net mvc的关系? 毕竟还是有区别的.
或者,我使用的是EntityFramework6.1.3和.NET Framework 4.5,vs2013(vs2012,vs2015应该也行),建立好.edmx后,PhoneBookModel.edmx已经‘嵌入输出程序集中‘了,.edmx文件包含了.ssdl,.csdl,.msl文件.而 H文说了半天,也就是把.ssdl,.csdl,.msl这三个文件属性的生成操作设为‘嵌入的资源‘. 唯一值得考究的就是PhoneBook.Views.cs视图文件了.它到底有多大作用呢?
希望明白的园友能和我多交流.