日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

entity framework6 edmx文件详解

發(fā)布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 entity framework6 edmx文件详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

entity framework中的edmx文件作為代碼與數(shù)據(jù)庫溝通的橋梁,作用是至關(guān)重要的。如果edmx文件出了問題,ef就基本上沒得用了。雖然edmx文件是由ef自動生成的,但是一些特定的操作可能會引發(fā)ef的bug,從而導致edmx文件出錯,并且無法使用“從數(shù)據(jù)庫更新模型”命令來修復,刪除edmx重建又要在新的edmx中重新聲明大量枚舉類型,這個時候,理解ef的內(nèi)部結(jié)構(gòu)就顯得很必要了。

我們創(chuàng)建一個簡單的數(shù)據(jù)庫,里面只有一張User表,表中有Id,Name,Type三個字段,其中Id為主鍵int型,Name為nvarchar型,最大長度為500,Type為int型,實體類中對應(yīng)為枚舉。

創(chuàng)建完畢后,用文本編輯器打開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>

只要認真看一遍,就會發(fā)現(xiàn)edmx文件主要分成了三大部分:數(shù)據(jù)庫部分、實體部分、映射部分。

數(shù)據(jù)庫部分為<edmx:StorageModels> (edmx存儲模型)標簽以內(nèi)的內(nèi)容,記錄了數(shù)據(jù)庫中各個表的表名、字段以及數(shù)據(jù)類型。

實體部分為<edmx:ConceptualModels>(edmx概念模型)標簽以內(nèi)的內(nèi)容,記錄了用來承載數(shù)據(jù)的實體類的相關(guān)信息。

映射部分為<edmx:Mappings>以內(nèi)的內(nèi)容,記錄了每張表與相應(yīng)的實體類中所有字段與屬性的映射關(guān)系。如果你嘗試在edmx模型視圖中修改一個字段并保存,再用文本編輯器打開edmx文件,你就會發(fā)現(xiàn)mapping部分中對應(yīng)的實體屬性發(fā)生了變化,而數(shù)據(jù)庫部分沒有發(fā)生改變。利用這個功能可以實現(xiàn)數(shù)據(jù)庫的字段名與實體的屬性名不一致但仍能映射,但不推薦這么做,因為你一旦刪除edmx重建,你就可能要再起一次別名。

接下來,我們嘗試往edmx模型中添加枚舉類。做法為edmx模型視圖上右鍵->模型瀏覽器->枚舉類型上添加新的枚舉類,最下方勾選引用外部的枚舉類型,填上之前定義好的枚舉類的命名空間+類名(如 Demo.Model.Type),確認后保存,再用文本編輯器打開edmx文件,就會發(fā)現(xiàn)實體部分多出了一條EnumType標簽。如:

<EnumType Name="Type" a:ExternalTypeName="EdmxTest.Enum.Type" xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" />

EnumType標簽往往出現(xiàn)在實體部分的末尾,也就是</Schema>標簽的上方,利用這一點我們可以很方便的實現(xiàn)Enum記錄的備份。

在大型項目多人協(xié)同開發(fā)的時候,如果某個成員對edmx文件進行了一些不正確的合并操作或者一些可能會引發(fā)bug的操作,edmx文件很可能會損壞,調(diào)用ef的時候就會報一些莫名其妙的錯誤。這個時候最簡單也是最快捷的方法,就是果斷的刪除edmx模型重建,而不要用文本編輯器去試圖解決問題。因為面對大型項目中上千行的edmx文件,用文本編輯器修好的可能性實在是太小了。

當添加一個edmx模型的時候你會發(fā)現(xiàn)系統(tǒng)自動為我們生成了***.desinger.cs、***.tt、***.Context.tt、***.edmx.diagram文件。desinger和edmx.diagram分別為舊版context和圖表文件,在開發(fā)中一般用處不大。其中desinger文件如果edmx啟用了舊的objectcontext策略就會生成內(nèi)容,并且需要項目引用system.data.entity程序集,否則會報錯。如果你不想引用,只需要在desinger文件上右鍵->屬性,生成操作選擇無即可。

總結(jié)

以上是生活随笔為你收集整理的entity framework6 edmx文件详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。