entity framework6 edmx文件详解
entity framework中的edmx文件作為代碼與數據庫溝通的橋梁,作用是至關重要的。如果edmx文件出了問題,ef就基本上沒得用了。雖然edmx文件是由ef自動生成的,但是一些特定的操作可能會引發ef的bug,從而導致edmx文件出錯,并且無法使用“從數據庫更新模型”命令來修復,刪除edmx重建又要在新的edmx中重新聲明大量枚舉類型,這個時候,理解ef的內部結構就顯得很必要了。
我們創建一個簡單的數據庫,里面只有一張User表,表中有Id,Name,Type三個字段,其中Id為主鍵int型,Name為nvarchar型,最大長度為500,Type為int型,實體類中對應為枚舉。
創建完畢后,用文本編輯器打開edmx文件,如下:
<?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx"><!-- EF Runtime content --><edmx:Runtime><!-- SSDL content --><edmx:StorageModels><Schema Namespace="edmxModel.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"><EntityType Name="User"><Key><PropertyRef Name="Id" /></Key><Property Name="Id" Type="int" Nullable="false" /><Property Name="Name" Type="nvarchar" MaxLength="500" /><Property Name="Type" Type="int" Nullable="false" /></EntityType><EntityContainer Name="edmxModelStoreContainer"><EntitySet Name="User" EntityType="Self.User" Schema="dbo" store:Type="Tables" /></EntityContainer></Schema></edmx:StorageModels><!-- CSDL content --><edmx:ConceptualModels><Schema Namespace="edmxModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm"><EntityType Name="User"><Key><PropertyRef Name="Id" /></Key><Property Name="Id" Type="Int32" Nullable="false" /><Property Name="Name" Type="String" MaxLength="500" FixedLength="false" Unicode="true" /><Property Name="Type" Type="Int32" Nullable="false" /></EntityType><EntityContainer Name="edmxEntities" annotation:LazyLoadingEnabled="true"><EntitySet Name="User" EntityType="Self.User" /></EntityContainer></Schema></edmx:ConceptualModels><!-- C-S mapping content --><edmx:Mappings><Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs"><EntityContainerMapping StorageEntityContainer="edmxModelStoreContainer" CdmEntityContainer="edmxEntities"><EntitySetMapping Name="User"><EntityTypeMapping TypeName="edmxModel.User"><MappingFragment StoreEntitySet="User"><ScalarProperty Name="Type" ColumnName="Type" /><ScalarProperty Name="Name" ColumnName="Name" /><ScalarProperty Name="Id" ColumnName="Id" /></MappingFragment></EntityTypeMapping></EntitySetMapping></EntityContainerMapping></Mapping></edmx:Mappings></edmx:Runtime><!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --><Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx"><Connection><DesignerInfoPropertySet><DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /></DesignerInfoPropertySet></Connection><Options><DesignerInfoPropertySet><DesignerProperty Name="ValidateOnBuild" Value="true" /><DesignerProperty Name="EnablePluralization" Value="false" /><DesignerProperty Name="IncludeForeignKeysInModel" Value="true" /><DesignerProperty Name="UseLegacyProvider" Value="false" /><DesignerProperty Name="CodeGenerationStrategy" Value="無" /></DesignerInfoPropertySet></Options><!-- Diagram content (shape and connector positions) --><Diagrams></Diagrams></Designer> </edmx:Edmx>只要認真看一遍,就會發現edmx文件主要分成了三大部分:數據庫部分、實體部分、映射部分。
數據庫部分為<edmx:StorageModels> (edmx存儲模型)標簽以內的內容,記錄了數據庫中各個表的表名、字段以及數據類型。
實體部分為<edmx:ConceptualModels>(edmx概念模型)標簽以內的內容,記錄了用來承載數據的實體類的相關信息。
映射部分為<edmx:Mappings>以內的內容,記錄了每張表與相應的實體類中所有字段與屬性的映射關系。如果你嘗試在edmx模型視圖中修改一個字段并保存,再用文本編輯器打開edmx文件,你就會發現mapping部分中對應的實體屬性發生了變化,而數據庫部分沒有發生改變。利用這個功能可以實現數據庫的字段名與實體的屬性名不一致但仍能映射,但不推薦這么做,因為你一旦刪除edmx重建,你就可能要再起一次別名。
接下來,我們嘗試往edmx模型中添加枚舉類。做法為edmx模型視圖上右鍵->模型瀏覽器->枚舉類型上添加新的枚舉類,最下方勾選引用外部的枚舉類型,填上之前定義好的枚舉類的命名空間+類名(如 Demo.Model.Type),確認后保存,再用文本編輯器打開edmx文件,就會發現實體部分多出了一條EnumType標簽。如:
<EnumType Name="Type" a:ExternalTypeName="EdmxTest.Enum.Type" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" />EnumType標簽往往出現在實體部分的末尾,也就是</Schema>標簽的上方,利用這一點我們可以很方便的實現Enum記錄的備份。
在大型項目多人協同開發的時候,如果某個成員對edmx文件進行了一些不正確的合并操作或者一些可能會引發bug的操作,edmx文件很可能會損壞,調用ef的時候就會報一些莫名其妙的錯誤。這個時候最簡單也是最快捷的方法,就是果斷的刪除edmx模型重建,而不要用文本編輯器去試圖解決問題。因為面對大型項目中上千行的edmx文件,用文本編輯器修好的可能性實在是太小了。
當添加一個edmx模型的時候你會發現系統自動為我們生成了***.desinger.cs、***.tt、***.Context.tt、***.edmx.diagram文件。desinger和edmx.diagram分別為舊版context和圖表文件,在開發中一般用處不大。其中desinger文件如果edmx啟用了舊的objectcontext策略就會生成內容,并且需要項目引用system.data.entity程序集,否則會報錯。如果你不想引用,只需要在desinger文件上右鍵->屬性,生成操作選擇無即可。
總結
以上是生活随笔為你收集整理的entity framework6 edmx文件详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信申请信用卡秒批吗
- 下一篇: 年轻人存款少、收入低,也能靠理财赚到钱,