Entity?Framework生成DataBase?First模式
一、新建控制臺應用程序,然后右鍵->添加新建項,選擇數(shù)據(jù)里面的實體數(shù)據(jù)模型:
然后點擊添加
二、選擇來自數(shù)據(jù)庫的EF設計器,并點擊下一步
三、在實體數(shù)據(jù)模型向?qū)Ы缑孢x擇要使用的數(shù)據(jù)連接,或者點擊新建連接按鈕創(chuàng)建新的連接,這里選擇已有的連接,并點擊下一步:
四、選擇實體框架6.0,點擊下一步:
五、選擇要操作的表,并點擊完成:
六、查看生成的項目結構
自動添加了EntityFramework的引用。同時會在項目的根目錄下面生成一個package文件夾:
package文件夾里面存放的是與EntityFramework有關的文件:
查看生成的配置文件:
ConfigSections節(jié)點里面有一個section段的name為entityFramework,下面有一個節(jié)點的名稱就是該section段name的值。如果想在其他地方使用EF的配置信息,那么需要把configSections、connectionStrings、entityFramework三個節(jié)點都需要拷貝過去,并且configSections節(jié)點是在所有節(jié)點的最前面。
七、查看生成的edmx文件
1、查看.tt下面的文件
.tt文件下面生成的就是數(shù)據(jù)庫表對應的實體類,并且是但是形式的。
2、查看.edmx文件
在edmx文件上面右鍵選擇打開方式,選擇XML(文本)編輯器方式打開:
文件的整個結構如下:
<?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ù)庫表的結構:
CSDL content定義的是實體類的結構:
C-S mapping content定義的是實體類和數(shù)據(jù)庫表的映射關系:
從C-S mapping content節(jié)點中可以看出,EF為什么會自動生成實體類,或者通過實體類為什么能操作數(shù)據(jù)庫的數(shù)據(jù)了。
從這里可以看出EF做的第一件事情:取數(shù)據(jù)庫的數(shù)據(jù)表結構,生成實體類,在表和實體之間自動建立映射關系。
3、StudentManager.tt是T4模板,是用來生成實體類的模板。
T4模板:從edmx中取數(shù)據(jù)結構,按照模板生成對應格式的實體類。
4、StudentManager.Context.cs文件
//------------------------------------------------------------------------------ // <auto-generated> // 此代碼已從模板生成。 // // 手動更改此文件可能導致應用程序出現(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模式的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Entity?Framework使用DataBase?First模式實現(xiàn)數(shù)據(jù)庫的增刪改查
- Entity Framework使用Code First模式管理事務
- Entity Framework管理并發(fā)
- Entity Framework使用Code First模式管理存儲過程
- Entity Framework使用Code First模式管理視圖
- Entity Framework加載控制Loading Entities
- Entity Framework使用LINQ操作實體
- Entity?Framework使用Code?First的實體繼承模式
- Entity Framework使用Code First模式管理數(shù)據(jù)庫
- Entity Framework表拆分為多個實體
- Entity?Framework管理一對二實體關系
- Entity?Framework管理一對一實體關系
- Entity?Framework實現(xiàn)數(shù)據(jù)遷移
- Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫
相關文章
ASP.NET?Core應用JWT進行用戶認證及Token的刷新方案
本文詳細講解了ASP.NET?Core應用JWT進行用戶認證及Token的刷新方案,文中通過示例代碼介紹的非常詳細。對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-12-12用存儲過程向數(shù)據(jù)庫存值的具體實現(xiàn)
本文為大家介紹下使用存儲過程向數(shù)據(jù)庫存值的具體實現(xiàn)過程,感興趣的朋友不要錯過2014-01-01asp.net 數(shù)據(jù)綁定時對數(shù)據(jù)列做個性化處理
asp.net 數(shù)據(jù)綁定時對數(shù)據(jù)列做個性化處理,需要的朋友可以參考下。2011-12-12asp.net使用Repeater控件中的全選進行批量操作實例
這篇文章主要介紹了asp.net使用Repeater控件中的全選進行批量操作方法,實例分析了Repeater控件的使用技巧,需要的朋友可以參考下2015-01-01.NET CORE3.1實現(xiàn)微信小程序發(fā)送訂閱消息
這篇文章主要介紹了.NET CORE3.1實現(xiàn)微信小程序發(fā)送訂閱消息,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09