Entity?Framework生成DataBase?First模式
一、新建控制臺(tái)應(yīng)用程序,然后右鍵->添加新建項(xiàng),選擇數(shù)據(jù)里面的實(shí)體數(shù)據(jù)模型:

然后點(diǎn)擊添加
二、選擇來(lái)自數(shù)據(jù)庫(kù)的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)的名稱(chēng)就是該section段name的值。如果想在其他地方使用EF的配置信息,那么需要把configSections、connectionStrings、entityFramework三個(gè)節(jié)點(diǎn)都需要拷貝過(guò)去,并且configSections節(jié)點(diǎn)是在所有節(jié)點(diǎn)的最前面。
七、查看生成的edmx文件
1、查看.tt下面的文件

.tt文件下面生成的就是數(shù)據(jù)庫(kù)表對(duì)應(yīng)的實(shí)體類(lèi),并且是但是形式的。
2、查看.edmx文件
在edmx文件上面右鍵選擇打開(kāi)方式,選擇XML(文本)編輯器方式打開(kāi):

文件的整個(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="無(wú)" />
</DesignerInfoPropertySet>
</Options>
<!-- Diagram content (shape and connector positions) -->
<Diagrams></Diagrams>
</Designer>
</edmx:Edmx>其中SSDL content定義的是數(shù)據(jù)庫(kù)表的結(jié)構(gòu):

CSDL content定義的是實(shí)體類(lèi)的結(jié)構(gòu):

C-S mapping content定義的是實(shí)體類(lèi)和數(shù)據(jù)庫(kù)表的映射關(guān)系:

從C-S mapping content節(jié)點(diǎn)中可以看出,EF為什么會(huì)自動(dòng)生成實(shí)體類(lèi),或者通過(guò)實(shí)體類(lèi)為什么能操作數(shù)據(jù)庫(kù)的數(shù)據(jù)了。
從這里可以看出EF做的第一件事情:取數(shù)據(jù)庫(kù)的數(shù)據(jù)表結(jié)構(gòu),生成實(shí)體類(lèi),在表和實(shí)體之間自動(dòng)建立映射關(guān)系。
3、StudentManager.tt是T4模板,是用來(lái)生成實(shí)體類(lèi)的模板。
T4模板:從edmx中取數(shù)據(jù)結(jié)構(gòu),按照模板生成對(duì)應(yīng)格式的實(shí)體類(lèi)。
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,用來(lái)操作數(shù)據(jù)庫(kù)。
到此這篇關(guān)于Entity Framework生成DataBase First模式的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Entity?Framework使用DataBase?First模式實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查
- Entity Framework使用Code First模式管理事務(wù)
- Entity Framework管理并發(fā)
- Entity Framework使用Code First模式管理存儲(chǔ)過(guò)程
- Entity Framework使用Code First模式管理視圖
- Entity Framework加載控制Loading Entities
- Entity Framework使用LINQ操作實(shí)體
- Entity?Framework使用Code?First的實(shí)體繼承模式
- Entity Framework使用Code First模式管理數(shù)據(jù)庫(kù)
- 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ù)庫(kù)
相關(guān)文章
.Net整合Json實(shí)現(xiàn)REST服務(wù)客戶(hù)端的方法詳解
這篇文章主要給大家介紹了關(guān)于.Net整合Json實(shí)現(xiàn)REST服務(wù)客戶(hù)端的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
ASP.NET MVC中將控制器分離到類(lèi)庫(kù)的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET MVC中將控制器分離到類(lèi)庫(kù)的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2015-06-06
ASP.NET中實(shí)現(xiàn)定制自己的委托和事件參數(shù)類(lèi)
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)定制自己的委托和事件參數(shù)類(lèi),需要的朋友可以參考下2014-08-08
Asp.Net Core 企業(yè)微信靜默授權(quán)的實(shí)現(xiàn)
這篇文章主要介紹了Asp.Net Core 企業(yè)微信靜默授權(quán)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
c#生成圖片縮略圖的類(lèi)(2種實(shí)現(xiàn)思路)
4個(gè)重載方法,有直接返回Image對(duì)象的,有生成縮略圖,并且保存到指定目錄的,具體祥看下文2013-05-05
jquery repeater 模仿 Google 展開(kāi)頁(yè)面預(yù)覽子視圖
節(jié)后的這一周, 希望大家能挺住, hehe, 這兩天給大家準(zhǔn)備一個(gè) Repeater 子視圖的例子, 模擬了 Google 搜索結(jié)果后的頁(yè)面的預(yù)覽, 其實(shí)也只是顯示了一段問(wèn)題2011-10-10
頁(yè)面間隔半秒鐘更新時(shí)間 Asp.net使用Comet開(kāi)發(fā)http長(zhǎng)連接示例分享
Comet(Reverse AJAX)主要是通過(guò)HTTP長(zhǎng)連接, 保持和服務(wù)器的連接,實(shí)現(xiàn)Server PUSH 和雙向通信,下面通過(guò)示例學(xué)習(xí)他的使用方法2014-01-01
ASP.NET MVC學(xué)習(xí)之NuGet在VS中的運(yùn)用淺談
這篇文章主要給大家介紹了關(guān)于ASP.NET MVC學(xué)習(xí)之NuGet在VS中運(yùn)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01

