使用EF的Code?First模式操作數(shù)據(jù)庫(kù)
EF的核心程序集位于System.Data.Entity.dll和System.Data.EntityFramework.dll中。
支持CodeFirst的位于EntityFramework.dll中。
通常使用NuGet Package Manager來(lái)添加這些程序集。
如果沒(méi)有數(shù)據(jù)庫(kù):
- 1、先寫(xiě)代碼,自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)。
- 2、如果代碼有變化,自動(dòng)刪除數(shù)據(jù)庫(kù)重建,或者是使用遷移功能更改已有數(shù)據(jù)庫(kù)。
如果已有數(shù)據(jù)庫(kù):
- 使用EF PowerTools反向工程生成模型。
下面的示例程序中將通過(guò)一個(gè)控制臺(tái)程序演示如何通過(guò)Code First模式創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),并執(zhí)行簡(jiǎn)單的增刪改查操作。
一、創(chuàng)建一個(gè)控制臺(tái)應(yīng)用程序,命名為CodeFirstAppDemo。

二、安裝Entity Framework,添加對(duì)Code First的支持
1、通過(guò)Nuget包管理器控制臺(tái)進(jìn)行安裝
選擇“工具”->Nuget程序包管理器->程序包管理器控制臺(tái),下面將會(huì)打開(kāi)程序包管理器控制臺(tái)窗口:

輸入命令:Install-Package EntityFramework進(jìn)行安裝。

2、通過(guò)可視化界面進(jìn)行安裝
在項(xiàng)目上面右鍵選擇管理Nuget程序包:

選擇EntityFramework,點(diǎn)擊“安裝”按鈕進(jìn)行安裝:

安裝完以后,項(xiàng)目引用里面將會(huì)出現(xiàn)EntityFramework程序集:

如果安裝完以后,項(xiàng)目引用里面沒(méi)有這兩個(gè)dll,一定要檢查為什么沒(méi)有安裝成功,因?yàn)橄旅娴某绦蛑幸玫紻bContext類(lèi),該類(lèi)位于EntityFramework程序集中。
三、根據(jù).NET中的類(lèi)來(lái)創(chuàng)建數(shù)據(jù)庫(kù)。
經(jīng)過(guò)上面的步驟之后,我們就可以開(kāi)始寫(xiě)代碼了。在寫(xiě)代碼之前,要始終記得:每個(gè)實(shí)體類(lèi)就是相應(yīng)的數(shù)據(jù)表中的一行數(shù)據(jù),該實(shí)體類(lèi)的屬性對(duì)應(yīng)的就是數(shù)據(jù)表中的列。
1、創(chuàng)建EDM實(shí)體數(shù)據(jù)模型
在項(xiàng)目上右鍵->添加->新建文件夾,命名為Models,存放相應(yīng)的實(shí)體類(lèi)。在Models文件夾下面新建兩個(gè)實(shí)體類(lèi):Category和Product,Category里面包含一個(gè)類(lèi)型是Product的集合屬性,兩個(gè)實(shí)體類(lèi)的屬性分別如下:
Category類(lèi):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeFirstAppDemo.Models
{
/// <summary>
/// 產(chǎn)品分類(lèi)表
/// </summary>
public class Category
{
/// <summary>
/// 分類(lèi)ID
/// </summary>
public int CategoryId { get; set; }
/// <summary>
/// 分類(lèi)名稱(chēng)
/// </summary>
public string CategoryName { get; set; }
/// <summary>
/// 產(chǎn)品
/// </summary>
public List<Product> ProductList { get; set; }
}
}Produce類(lèi):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeFirstAppDemo.Models
{
/// <summary>
/// 產(chǎn)品類(lèi)
/// </summary>
public class Product
{
/// <summary>
/// 產(chǎn)品Id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 產(chǎn)品名稱(chēng)
/// </summary>
public string ProductName { get; set; }
/// <summary>
/// 產(chǎn)品價(jià)格
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 出版日期
/// </summary>
public DateTime PublicDate { get; set; }
}
}我們需要定義和期望的數(shù)據(jù)庫(kù)類(lèi)型相匹配的屬性。上面的例子中,.Net中的int類(lèi)型會(huì)映射到SQL Server中的int類(lèi)型,string類(lèi)型會(huì)映射到所有可能的字符類(lèi)型,decimal和Datetime也和SQL Server中的一樣。大多數(shù)時(shí)候,我們不需要關(guān)心這些細(xì)節(jié),我們只需要編寫(xiě)能夠表示數(shù)據(jù)的模型類(lèi)就行了,然后使用標(biāo)準(zhǔn)的.Net類(lèi)型定義屬性,其他的就讓EF自己計(jì)算出保存數(shù)據(jù)所需要的RDBMS類(lèi)型。
2、創(chuàng)建數(shù)據(jù)上下文
接下來(lái)我們創(chuàng)建數(shù)據(jù)庫(kù)上下文,它是數(shù)據(jù)庫(kù)的抽象。目前,我們有兩張張表Category和Product,因而要給該數(shù)據(jù)庫(kù)上下文定義兩個(gè)屬性來(lái)代表這兩張表。再者,一張表中一般肯定不止一條數(shù)據(jù)行,所以我們必須定義一個(gè)集合屬性,EF使用DbSet來(lái)實(shí)現(xiàn)這個(gè)目的。
在項(xiàng)目上右鍵->添加->新建文件夾,命名為EFDbContext,用來(lái)存放數(shù)據(jù)庫(kù)上下文類(lèi)。添加類(lèi)Context,并使該類(lèi)繼承自DbContext類(lèi)。DbContext位于EntityFramework.dll程序集中。
Context類(lèi)代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using CodeFirstAppDemo.Models;
namespace CodeFirstAppDemo.EFDbContext
{
public class Context : DbContext
{
/// <summary>
/// 1、創(chuàng)建構(gòu)造函數(shù),構(gòu)造函數(shù)繼承DbContext類(lèi)的構(gòu)造函數(shù),通過(guò)DbContext類(lèi)的構(gòu)造函數(shù)創(chuàng)建數(shù)據(jù)庫(kù)連接
/// 2、DbContext類(lèi)的構(gòu)造函數(shù)里面的參數(shù)是數(shù)據(jù)庫(kù)連接字符串,通過(guò)該連接字符串去創(chuàng)建數(shù)據(jù)庫(kù)
/// </summary>
public Context()
: base("name=FirstCodeFirstApp")
{ }
//2、定義數(shù)據(jù)集合:用于創(chuàng)建表
public DbSet<Category> Categorys { get; set; }
public DbSet<Product> Products { get; set; }
}
}在這里,DbContext是所有基于EF的上下文基類(lèi),通過(guò)它可以訪問(wèn)到數(shù)據(jù)庫(kù)中的所有表。上面的代碼中調(diào)用了父類(lèi)的構(gòu)造函數(shù),并且傳入了一個(gè)鍵值對(duì),鍵是name,值是CodeFirstApp,這個(gè)鍵值對(duì)是定義在應(yīng)用程序的配置文件中的,取決于你的應(yīng)用程序類(lèi)型,可能是app.config或者web.config。在我們的控制臺(tái)應(yīng)用程序中就是app.config。
在app.config文件的configuration節(jié)點(diǎn)下(不要在第一個(gè)節(jié)點(diǎn)下,否則會(huì)報(bào)錯(cuò))添加:
<connectionStrings>
<add name="CodeFirstApp" connectionString="Server=.;Database=CodeFirstApp;User Id=sa;Password=test" providerName="System.Data.SqlClient"/>
</connectionStrings>3、使用EF提供的API訪問(wèn)數(shù)據(jù)庫(kù)來(lái)創(chuàng)建數(shù)據(jù)庫(kù)
using CodeFirstAppDemo.EFDbContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeFirstAppDemo
{
class Program
{
static void Main(string[] args)
{
// 使用數(shù)據(jù)庫(kù)上下文Context
using (var context = new Context())
{
// 如果數(shù)據(jù)庫(kù)不存在,則調(diào)用EF內(nèi)置的API創(chuàng)建數(shù)據(jù)庫(kù)
if (context.Database.CreateIfNotExists())
{
Console.WriteLine("數(shù)據(jù)庫(kù)創(chuàng)建成功!");
}
else
{
Console.WriteLine("數(shù)據(jù)庫(kù)已存在");
}
}
Console.ReadKey();
}
}
}最后,只需要確保連接字符串沒(méi)有問(wèn)題就可以了。運(yùn)行程序,然后打開(kāi)SSMS進(jìn)行確認(rèn)數(shù)據(jù)庫(kù)是否創(chuàng)建成功即可。


這時(shí)可以清楚地看到,數(shù)據(jù)庫(kù)表名是自定義數(shù)據(jù)庫(kù)上下文中DbSet<T>屬性中T類(lèi)型的復(fù)數(shù)形式。例如T類(lèi)型是Product,那么生成的表名就是Products,而表中的列是數(shù)據(jù)模型的屬性。此外,注意以下列的類(lèi)型。EF默認(rèn)將id作為了主鍵,string類(lèi)型的ProductName在數(shù)據(jù)庫(kù)中的類(lèi)型是nvarchar(max),這些都是在使用EF時(shí)必須注意的命名規(guī)格。
四、執(zhí)行簡(jiǎn)單的CRUD操作
1、創(chuàng)建記錄-Create
你可以這樣認(rèn)為,將對(duì)象添加到集合中就相當(dāng)于將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)相應(yīng)的表中。我們使用DbSet的Add方法來(lái)實(shí)現(xiàn)新數(shù)據(jù)的添加,而DbContext類(lèi)的SaveChanges方法會(huì)將未處理的更改提交到數(shù)據(jù)庫(kù),這是通過(guò)檢測(cè)上下文中所有的對(duì)象的狀態(tài)來(lái)完成的。所有的對(duì)象都駐留在上下文類(lèi)的DbSet屬性中。比如,例子中有一個(gè)Products屬性,那么所有的產(chǎn)品數(shù)據(jù)都會(huì)存儲(chǔ)到這個(gè)泛型集合屬性中。數(shù)據(jù)庫(kù)上下文會(huì)跟蹤DbSet屬性中的所有對(duì)象的狀態(tài),這些狀態(tài)有這么幾種:Deleted、Added、Modified和Unchanged。如果你想在一個(gè)表中插入多行數(shù)據(jù),那么只需要添加該表對(duì)應(yīng)的類(lèi)的多個(gè)對(duì)象的實(shí)例即可,然后使用SaveChanges方法將更改提交到數(shù)據(jù)庫(kù),該方法是以單事務(wù)執(zhí)行的。最終,所有的數(shù)據(jù)庫(kù)更改都會(huì)以單個(gè)工作單元持久化。既然是事務(wù),那么就允許將批量相關(guān)的更改作為單個(gè)操作提交,這樣就保證了事務(wù)一致性和數(shù)據(jù)完整性。
修改Main方法如下:
using CodeFirstAppDemo.EFDbContext;
using CodeFirstAppDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeFirstAppDemo
{
class Program
{
static void Main(string[] args)
{
// 使用數(shù)據(jù)庫(kù)上下文Context
using (var context = new Context())
{
// 如果數(shù)據(jù)庫(kù)不存在,則調(diào)用EF內(nèi)置的API創(chuàng)建數(shù)據(jù)庫(kù)
if (context.Database.CreateIfNotExists())
{
Console.WriteLine("數(shù)據(jù)庫(kù)創(chuàng)建成功!");
}
else
{
Console.WriteLine("數(shù)據(jù)庫(kù)已存在");
}
#region EF 添加數(shù)據(jù)
//添加數(shù)據(jù)
var cate = new List<Category> {
new Category{
CategoryName="文學(xué)類(lèi)",
ProductList=new List<Product>{
new Product
{
ProductName="百年孤獨(dú)",
Price=37.53m,
PublicDate=new DateTime(2011,6,1)
},
new Product
{
ProductName="老人與海",
Price=37.53m,
PublicDate=new DateTime(2010,6,1)
}
}
},
new Category{
CategoryName="計(jì)算機(jī)類(lèi)",
ProductList=new List<Product>{
new Product
{
ProductName="C#高級(jí)編程第九版",
Price=48.23m,
PublicDate=new DateTime(2016,2,8)
},
new Product
{
ProductName="Oracle從入門(mén)到精通",
Price=27.03m,
PublicDate=new DateTime(2014,7,9)
}
}
}
};
//將創(chuàng)建的集合添加到上下文中
context.Categorys.AddRange(cate);
//調(diào)用SaveChanges()方法,將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)
context.SaveChanges();
#endregion
}
Console.ReadKey();
}
}
}這里需要注意兩點(diǎn):
1、不需要給Product.Id屬性賦值,因?yàn)樗鼘?duì)應(yīng)到SQL Server表中的主鍵列,它的值是自動(dòng)生成的,當(dāng)SaveChanges執(zhí)行以后,打斷點(diǎn)就能看到返回的Product.Id已經(jīng)有值了。
2、Context的實(shí)例用了using語(yǔ)句包裝起來(lái),這是因?yàn)镈bContext實(shí)現(xiàn)了IDisposable接口。DbContext還包含了DbConnection的實(shí)例,該實(shí)例指向了具有特定連接字符串的數(shù)據(jù)庫(kù)。在EF中合適地釋放數(shù)據(jù)庫(kù)連接和ADO.NET中同等重要。
2、查詢(xún)記錄-Retrieve
查詢(xún)時(shí)也是直接通過(guò)DbSet進(jìn)行查詢(xún)的:
using CodeFirstAppDemo.EFDbContext;
using CodeFirstAppDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeFirstAppDemo
{
class Program
{
static void Main(string[] args)
{
// 使用數(shù)據(jù)庫(kù)上下文Context
using (var context = new Context())
{
// 如果數(shù)據(jù)庫(kù)不存在,則調(diào)用EF內(nèi)置的API創(chuàng)建數(shù)據(jù)庫(kù)
if (context.Database.CreateIfNotExists())
{
Console.WriteLine("數(shù)據(jù)庫(kù)創(chuàng)建成功!");
}
else
{
Console.WriteLine("數(shù)據(jù)庫(kù)已存在");
}
#region EF 2查詢(xún)數(shù)據(jù)
//查詢(xún)方式1
var products = from p in context.Categorys select p;
foreach (var item in products)
{
Console.WriteLine("分類(lèi)名稱(chēng):" + item.CategoryName);
}
//查詢(xún)方式2
//延遲加載 cates里面沒(méi)有數(shù)據(jù)
var cates = context.Categorys;
//執(zhí)行迭代的時(shí)候才有數(shù)據(jù)
foreach (var item in cates)
{
Console.WriteLine("分類(lèi)名稱(chēng):" + item.CategoryName);
}
#endregion
}
Console.ReadKey();
}
}
}如果像下面那樣打一個(gè)斷點(diǎn),你會(huì)看到一個(gè)結(jié)果視圖,點(diǎn)擊類(lèi)似刷新的圖標(biāo)會(huì)看到查詢(xún)的結(jié)果,這個(gè)東西道出了EF中很重要的一個(gè)概念:延遲加載。此時(shí)還沒(méi)有真正查詢(xún)數(shù)據(jù)庫(kù),只有當(dāng)LINQ的查詢(xún)結(jié)果被訪問(wèn)或者被枚舉時(shí)才會(huì)將查詢(xún)命令發(fā)送到數(shù)據(jù)庫(kù)。EF是基于DbSet實(shí)現(xiàn)的IQueryable接口來(lái)處理延遲查詢(xún)的。
3、更新記錄-Update
在SQL中,更新需要執(zhí)行Update命令。而在EF中,我們要找到DbSet實(shí)體集合中要更新的對(duì)象,然后修改其屬性,最后調(diào)用SaveChanges方法即可。
using CodeFirstAppDemo.EFDbContext;
using CodeFirstAppDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeFirstAppDemo
{
class Program
{
static void Main(string[] args)
{
// 使用數(shù)據(jù)庫(kù)上下文Context
using (var context = new Context())
{
// 如果數(shù)據(jù)庫(kù)不存在,則調(diào)用EF內(nèi)置的API創(chuàng)建數(shù)據(jù)庫(kù)
if (context.Database.CreateIfNotExists())
{
Console.WriteLine("數(shù)據(jù)庫(kù)創(chuàng)建成功!");
}
else
{
Console.WriteLine("數(shù)據(jù)庫(kù)已存在");
}
#region EF 更新數(shù)據(jù)
var products = context.Products;
if (products.Any())
{
// 查詢(xún)產(chǎn)品名稱(chēng)是“百年孤獨(dú)”的產(chǎn)品
var toUpdateProduct = products.First(p => p.ProductName == "百年孤獨(dú)");
// 修改查詢(xún)出的產(chǎn)品名稱(chēng)
toUpdateProduct.ProductName = "唐詩(shī)三百首";
// 調(diào)用SaveChanges()方法保存數(shù)據(jù)
context.SaveChanges();
}
#endregion
}
Console.ReadKey();
}
}
}這里我們使用了Any()擴(kuò)展方法來(lái)判斷序列中是否有元素,然后使用First()擴(kuò)展方法來(lái)找到Name=="百年孤獨(dú)"的元素,然后給目標(biāo)對(duì)象的Name屬性賦予新值,最后調(diào)用SaveChanges()方法保存數(shù)據(jù)。
4、刪除記錄-Delete
要?jiǎng)h除一條數(shù)據(jù),就要先找到這條數(shù)據(jù).
using CodeFirstAppDemo.EFDbContext;
using CodeFirstAppDemo.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
namespace CodeFirstAppDemo
{
class Program
{
static void Main(string[] args)
{
// 使用數(shù)據(jù)庫(kù)上下文Context
using (var context = new Context())
{
// 如果數(shù)據(jù)庫(kù)不存在,則調(diào)用EF內(nèi)置的API創(chuàng)建數(shù)據(jù)庫(kù)
if (context.Database.CreateIfNotExists())
{
Console.WriteLine("數(shù)據(jù)庫(kù)創(chuàng)建成功!");
}
else
{
Console.WriteLine("數(shù)據(jù)庫(kù)已存在");
}
#region EF 刪除數(shù)據(jù)
var products = context.Products;
// 先根據(jù)ProductName找到要?jiǎng)h除的元素
var toDeleteProduct = context.Products.Single(p => p.ProductName == "唐詩(shī)三百首");
if (toDeleteProduct != null)
{
// 方式1:使用Remove()方法移除
context.Products.Remove(toDeleteProduct);
// 方式2:更改數(shù)據(jù)的狀態(tài)
context.Entry(toDeleteProduct).State = EntityState.Deleted;
// 最后持久化到數(shù)據(jù)庫(kù)
context.SaveChanges();
#endregion
}
Console.ReadKey();
}
}
}到此這篇關(guān)于使用EF的Code First模式操作數(shù)據(jù)庫(kù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.net中string類(lèi)型可以作為lock的鎖對(duì)象嗎
lock 關(guān)鍵字是用于在多線程編程中實(shí)現(xiàn)同步和互斥訪問(wèn)的關(guān)鍵字,它的作用是確保共享資源在任意時(shí)刻只能被一個(gè)線程訪問(wèn),從而避免出現(xiàn)競(jìng)態(tài)條件(race condition)和數(shù)據(jù)不一致的問(wèn)題,這篇文章主要介紹了string類(lèi)型可以作為lock的鎖對(duì)象嗎,需要的朋友可以參考下2023-06-06
Asp.net core WebApi 使用Swagger生成幫助頁(yè)實(shí)例
本篇文章主要介紹了Asp.net core WebApi 使用Swagger生成幫助頁(yè)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
.Net Core中使用Autofac替換自帶的DI容器的示例
Autofac比Core中自帶的DI功能強(qiáng)大的多,比如:屬性注入、基于名稱(chēng)注入、子容器、自定生存期管理、遲緩初始化,本文就詳細(xì)的來(lái)介紹一下.Net Core Autofac替換DI容器,感興趣的可以了解一下2021-06-06
Asp.net簡(jiǎn)單代碼設(shè)置GridView自適應(yīng)列寬不變形實(shí)現(xiàn)思路與代碼
動(dòng)態(tài)綁定的GridView由于列數(shù)不固定,而列又太多(博主做的這個(gè)項(xiàng)目有150個(gè)左右的字段),這樣設(shè)置GridView固定寬度就不能滿足需求了2013-01-01
System.Web.Routing入門(mén)及進(jìn)階
上面介紹的是最簡(jiǎn)單的一種定義方式。當(dāng)然我們可以建立更復(fù)雜的規(guī)則。其中就包括設(shè)定規(guī)則的默認(rèn)值以及設(shè)定規(guī)則的正則表達(dá)式2011-12-12
ASP.NET將文件寫(xiě)到另一服務(wù)器(圖文教程)及注意事項(xiàng)
有時(shí)我們需要將來(lái)自于客戶端的文件上傳到WEB服務(wù)器端,并在服務(wù)端將文件存儲(chǔ)到第三方文件服務(wù)器中存儲(chǔ),既然有需求,那就有實(shí)現(xiàn)了,感興趣的你可以了解此文,或許對(duì)你學(xué)習(xí)asp.net 起到很好的作用哦2013-01-01

