您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Entity Framework中如何使用DataBase First模式實(shí)現(xiàn)增刪改查”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Entity Framework中如何使用DataBase First模式實(shí)現(xiàn)增刪改查”吧!
然后點(diǎn)擊添加
自動添加了EntityFramework的引用。同時會在項(xiàng)目的根目錄下面生成一個package文件夾:
package文件夾里面存放的是與EntityFramework有關(guān)的文件:
查看生成的配置文件:
ConfigSections節(jié)點(diǎn)里面有一個section段的name為entityFramework,下面有一個節(jié)點(diǎn)的名稱就是該section段name的值。如果想在其他地方使用EF的配置信息,那么需要把configSections、connectionStrings、entityFramework三個節(jié)點(diǎn)都需要拷貝過去,并且configSections節(jié)點(diǎn)是在所有節(jié)點(diǎn)的最前面。
.tt文件下面生成的就是數(shù)據(jù)庫表對應(yīng)的實(shí)體類,并且是但是形式的。
在edmx文件上面右鍵選擇打開方式,選擇XML(文本)編輯器方式打開:
文件的整個結(jié)構(gòu)如下:
<?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="StudentSystemModel.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="Student"> <Key> <PropertyRef Name="StudentID" /> </Key> <Property Name="StudentID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" /> <Property Name="StudentName" Type="varchar" MaxLength="16" /> <Property Name="Sex" Type="varchar" MaxLength="8" /> <Property Name="Age" Type="int" /> <Property Name="Major" Type="varchar" MaxLength="32" /> <Property Name="Email" Type="varchar" MaxLength="32" /> </EntityType> <EntityContainer Name="StudentSystemModelStoreContainer"> <EntitySet Name="Student" EntityType="Self.Student" Schema="dbo" store:Type="Tables" /> </EntityContainer> </Schema> </edmx:StorageModels> <!-- CSDL content --> <edmx:ConceptualModels> <Schema Namespace="StudentSystemModel" 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="Student"> <Key> <PropertyRef Name="StudentID" /> </Key> <Property Name="StudentID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Name="StudentName" Type="String" MaxLength="16" FixedLength="false" Unicode="false" /> <Property Name="Sex" Type="String" MaxLength="8" FixedLength="false" Unicode="false" /> <Property Name="Age" Type="Int32" /> <Property Name="Major" Type="String" MaxLength="32" FixedLength="false" Unicode="false" /> <Property Name="Email" Type="String" MaxLength="32" FixedLength="false" Unicode="false" /> </EntityType> <EntityContainer Name="StudentSystemEntities" annotation:LazyLoadingEnabled="true"> <EntitySet Name="Students" EntityType="Self.Student" /> </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="StudentSystemModelStoreContainer" CdmEntityContainer="StudentSystemEntities"> <EntitySetMapping Name="Students"> <EntityTypeMapping TypeName="StudentSystemModel.Student"> <MappingFragment StoreEntitySet="Student"> <ScalarProperty Name="StudentID" ColumnName="StudentID" /> <ScalarProperty Name="StudentName" ColumnName="StudentName" /> <ScalarProperty Name="Sex" ColumnName="Sex" /> <ScalarProperty Name="Age" ColumnName="Age" /> <ScalarProperty Name="Major" ColumnName="Major" /> <ScalarProperty Name="Email" ColumnName="Email" /> </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="true" /> <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>
其中SSDL content定義的是數(shù)據(jù)庫表的結(jié)構(gòu):
CSDL content定義的是實(shí)體類的結(jié)構(gòu):
C-S mapping content定義的是實(shí)體類和數(shù)據(jù)庫表的映射關(guān)系:
從C-S mapping content節(jié)點(diǎn)中可以看出,EF為什么會自動生成實(shí)體類,或者通過實(shí)體類為什么能操作數(shù)據(jù)庫的數(shù)據(jù)了。
從這里可以看出EF做的第一件事情:取數(shù)據(jù)庫的數(shù)據(jù)表結(jié)構(gòu),生成實(shí)體類,在表和實(shí)體之間自動建立映射關(guān)系。
T4模板:從edmx中取數(shù)據(jù)結(jié)構(gòu),按照模板生成對應(yīng)格式的實(shí)體類。
//------------------------------------------------------------------------------ // <auto-generated> // 此代碼已從模板生成。 // // 手動更改此文件可能導(dǎo)致應(yīng)用程序出現(xiàn)意外的行為。 // 如果重新生成代碼,將覆蓋對此文件的手動更改。 // </auto-generated> //------------------------------------------------------------------------------ namespace EFDbFirstDemo { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class StudentSystemEntities : DbContext { public StudentSystemEntities() : base("name=StudentSystemEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Student> Students { get; set; } } }
StudentSystemEntities繼承自DbContext,用來操作數(shù)據(jù)庫。
到此,相信大家對“Entity Framework中如何使用DataBase First模式實(shí)現(xiàn)增刪改查”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。