Entity?Framework生成DataBase?First模式
一、新建控制臺(tái)應(yīng)用程序,然后右鍵->添加新建項(xiàng),選擇數(shù)據(jù)里面的實(shí)體數(shù)據(jù)模型:
然后點(diǎn)擊添加
二、選擇來自數(shù)據(jù)庫的EF設(shè)計(jì)器,并點(diǎn)擊下一步
三、在實(shí)體數(shù)據(jù)模型向?qū)Ы缑孢x擇要使用的數(shù)據(jù)連接,或者點(diǎn)擊新建連接按鈕創(chuàng)建新的連接,這里選擇已有的連接,并點(diǎn)擊下一步:
四、選擇實(shí)體框架6.0,點(diǎn)擊下一步:
五、選擇要操作的表,并點(diǎn)擊完成:
六、查看生成的項(xiàng)目結(jié)構(gòu)
自動(dòng)添加了EntityFramework的引用。同時(shí)會(huì)在項(xiàng)目的根目錄下面生成一個(gè)package文件夾:
package文件夾里面存放的是與EntityFramework有關(guān)的文件:
查看生成的配置文件:
ConfigSections節(jié)點(diǎn)里面有一個(gè)section段的name為entityFramework,下面有一個(gè)節(jié)點(diǎn)的名稱就是該section段name的值。如果想在其他地方使用EF的配置信息,那么需要把configSections、connectionStrings、entityFramework三個(gè)節(jié)點(diǎn)都需要拷貝過去,并且configSections節(jié)點(diǎn)是在所有節(jié)點(diǎn)的最前面。
七、查看生成的edmx文件
1、查看.tt下面的文件
.tt文件下面生成的就是數(shù)據(jù)庫表對(duì)應(yīng)的實(shí)體類,并且是但是形式的。
2、查看.edmx文件
在edmx文件上面右鍵選擇打開方式,選擇XML(文本)編輯器方式打開:
文件的整個(gè)結(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為什么會(huì)自動(dòng)生成實(shí)體類,或者通過實(shí)體類為什么能操作數(shù)據(jù)庫的數(shù)據(jù)了。
從這里可以看出EF做的第一件事情:取數(shù)據(jù)庫的數(shù)據(jù)表結(jié)構(gòu),生成實(shí)體類,在表和實(shí)體之間自動(dòng)建立映射關(guān)系。
3、StudentManager.tt是T4模板,是用來生成實(shí)體類的模板。
T4模板:從edmx中取數(shù)據(jù)結(jié)構(gòu),按照模板生成對(duì)應(yīng)格式的實(shí)體類。
4、StudentManager.Context.cs文件
//------------------------------------------------------------------------------ // <auto-generated> // 此代碼已從模板生成。 // // 手動(dòng)更改此文件可能導(dǎo)致應(yīng)用程序出現(xiàn)意外的行為。 // 如果重新生成代碼,將覆蓋對(duì)此文件的手動(dòng)更改。 // </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ù)庫。
到此這篇關(guān)于Entity Framework生成DataBase First模式的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Entity?Framework使用DataBase?First模式實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查
- Entity Framework使用Code First模式管理事務(wù)
- Entity Framework管理并發(fā)
- Entity Framework使用Code First模式管理存儲(chǔ)過程
- Entity Framework使用Code First模式管理視圖
- Entity Framework加載控制Loading Entities
- Entity Framework使用LINQ操作實(shí)體
- Entity?Framework使用Code?First的實(shí)體繼承模式
- Entity Framework使用Code First模式管理數(shù)據(jù)庫
- Entity Framework表拆分為多個(gè)實(shí)體
- Entity?Framework管理一對(duì)二實(shí)體關(guān)系
- Entity?Framework管理一對(duì)一實(shí)體關(guān)系
- Entity?Framework實(shí)現(xiàn)數(shù)據(jù)遷移
- Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫
相關(guān)文章
asp.net驗(yàn)證一個(gè)字符串是否符合指定的正則表達(dá)式
asp.net檢驗(yàn)字符串是否滿足指定正則表達(dá)式2008-05-05進(jìn)度條在.net導(dǎo)入Excel時(shí)的應(yīng)用實(shí)例
這篇文章主要介紹了進(jìn)度條在.net導(dǎo)入Excel時(shí)的應(yīng)用,以實(shí)例形式講述了.net導(dǎo)入Excel時(shí)根據(jù)頁面情況顯示進(jìn)度條的實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10ASP.NET?Core應(yīng)用JWT進(jìn)行用戶認(rèn)證及Token的刷新方案
本文詳細(xì)講解了ASP.NET?Core應(yīng)用JWT進(jìn)行用戶認(rèn)證及Token的刷新方案,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12用存儲(chǔ)過程向數(shù)據(jù)庫存值的具體實(shí)現(xiàn)
本文為大家介紹下使用存儲(chǔ)過程向數(shù)據(jù)庫存值的具體實(shí)現(xiàn)過程,感興趣的朋友不要錯(cuò)過2014-01-01asp.net和ajax實(shí)現(xiàn)智能搜索功能代碼
近來一直在開發(fā)股票模擬系統(tǒng),終于告一段落了,回想起來感慨很多。突然想應(yīng)該做點(diǎn)總結(jié)了,想來想去還是覺得通過寫點(diǎn)日志來把相關(guān)的知識(shí)點(diǎn)記錄下來,下面就我在項(xiàng)目中經(jīng)常用到的動(dòng)態(tài)提示搜索選項(xiàng)功能的實(shí)現(xiàn)。2010-03-03淺談.Net Core 認(rèn)證系統(tǒng)源碼解析
這篇文章主要介紹了淺談.Net Core 認(rèn)證系統(tǒng)源碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12asp.net 數(shù)據(jù)綁定時(shí)對(duì)數(shù)據(jù)列做個(gè)性化處理
asp.net 數(shù)據(jù)綁定時(shí)對(duì)數(shù)據(jù)列做個(gè)性化處理,需要的朋友可以參考下。2011-12-12asp.net使用Repeater控件中的全選進(jìn)行批量操作實(shí)例
這篇文章主要介紹了asp.net使用Repeater控件中的全選進(jìn)行批量操作方法,實(shí)例分析了Repeater控件的使用技巧,需要的朋友可以參考下2015-01-01.NET CORE3.1實(shí)現(xiàn)微信小程序發(fā)送訂閱消息
這篇文章主要介紹了.NET CORE3.1實(shí)現(xiàn)微信小程序發(fā)送訂閱消息,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09