1.傳統(tǒng)修改模式,看下列代碼
復(fù)制代碼 代碼如下:
using (NorthwindEntities context = new NorthwindEntities())
{
Region region = context.Region.FirstOrDefault(v => v.RegionID == 4);
region.RegionDescription = "Test";
context.SaveChanges();
}
監(jiān)控SQL語句:
復(fù)制代碼 代碼如下:
SQL1:SELECT TOP 1 [Extent1].[RegionID] AS [RegionID], [Extent1].[RegionDescription] AS [RegionDescription]FROM [dbo].[Region] AS [Extent1]WHERE 4 = [Extent1].[RegionID]
SQL2:exec sp_executesql N'update [dbo].[Region]set [RegionDescription] = @0where ([RegionID] = @1)', N'@0 nchar(4),@1 int', @0 = N'Test', @1 = 4
從這里例子中可以看出使用“傳統(tǒng)模式”的數(shù)據(jù)更新,必須先要執(zhí)行一次查詢,將獲取要更新的實(shí)體對(duì)象,在看下面的例子:
復(fù)制代碼 代碼如下:
Region region;
using (NorthwindEntities context = new NorthwindEntities())
{
region = context.Region.FirstOrDefault(v => v.RegionID == 4);
}
using (NorthwindEntities context = new NorthwindEntities())
{
region.RegionDescription = "Test";
context.SaveChanges();
}
更新是不會(huì)執(zhí)行的,因?yàn)閷?shí)體不再 執(zhí)行 SaveChanges 的對(duì)象中所以 當(dāng)我們更新一個(gè) 不再當(dāng)前連接中的對(duì)象是必須要先執(zhí)行查詢獲取這個(gè)對(duì)象才能對(duì)其更新,如下:
復(fù)制代碼 代碼如下:
Region region;
using (NorthwindEntities context = new NorthwindEntities())
{
region = context.Region.FirstOrDefault(v => v.RegionID == 4);
}
using (NorthwindEntities context = new NorthwindEntities())
{
Region newRegion = context.Region.FirstOrDefault(v => v.RegionID == region.RegionID);
region.RegionDescription = "Test";
context.SaveChanges();
}
2.使用ApplyPropertyChanges 修改實(shí)體
復(fù)制代碼 代碼如下:
Region region;
using (NorthwindEntities ne = new NorthwindEntities())
{
//利用EntityObject.Execute(MergeOption.NoTracking),等效于使用ObjectContext.Dettach(EntityObject)
//查詢并分離對(duì)象
region = ne.Region.Execute(MergeOption.NoTracking).Where(v => v.RegionID == 1).FirstOrDefault();
}
//修改分離的值
region.RegionDescription = "TestTest1";
//使用分離的對(duì)象 order 更新
using (NorthwindEntities context = new NorthwindEntities())
{
//將數(shù)據(jù)載入到context中以便更新
context.GetObjectByKey(region.EntityKey);
//使用order 更新 context中的對(duì)應(yīng)對(duì)象
context.ApplyPropertyChanges(region.EntityKey.EntitySetName, region);
context.SaveChanges();
}
監(jiān)控SQL語句:
復(fù)制代碼 代碼如下:
SQL1:exec sp_executesql N'SELECT [Extent1].[RegionID] AS [RegionID], [Extent1].[RegionDescription] AS [RegionDescription] FROM [dbo].[Region] AS [Extent1] WHERE [Extent1].[RegionID] = @p0', N'@p0 int', @p0 = 1
SQL2:exec sp_executesql N'update [dbo].[Region] set [RegionDescription] = @0where ([RegionID] = @1) ', N'@0 nchar(9),@1 int', @0 = N'TestTest1', @1 = 1
ApplyPropertyChanges在MSDN的解釋是“將已分離對(duì)象的屬性更改應(yīng)用于已附加到對(duì)象上下文的對(duì)象?!逼鋵?shí)說白了就是 拿舊對(duì)象去更新新對(duì)象,我們可以看出 使用“ApplyPropertyChanges 修改實(shí)體”方法修改實(shí)體與 使用“傳統(tǒng)模式”是一樣的,都是必須先執(zhí)行一次查詢,獲取更新的對(duì)象,但是 ApplyPropertyChanges方法的特殊之處是,該方法會(huì)拿內(nèi)存中的對(duì)象(新對(duì)象)和當(dāng)前連接中的對(duì)象(舊對(duì)象)對(duì)比,自動(dòng)生成對(duì)應(yīng)字段修改的Update語句,如果內(nèi)存中的對(duì)象與當(dāng)前連接中的對(duì)象完全相等(每個(gè)字段的值都相等),將不生成響應(yīng)的Update。當(dāng)我們?cè)俅螆?zhí)行 上述代碼觀察監(jiān)控到了SQL語句,你會(huì)發(fā)現(xiàn)只監(jiān)控到SQL1,不會(huì)得到SQL2。
3.使用Attach與SetModifiedProperty修改實(shí)體
復(fù)制代碼 代碼如下:
using (NorthwindEntities context = new NorthwindEntities())
{
Region region = context.Region.FirstOrDefault(v => v.RegionID == 4);
context.Detach(region);
region.RegionDescription = "因?yàn)闇y(cè)試";
context.Attach(region);
var newRegion = context.ObjectStateManager.GetObjectStateEntry(region);
newRegion.SetModified();
newRegion.SetModifiedProperty("RegionDescription");
context.SaveChanges();
}
監(jiān)視SQL語句:
復(fù)制代碼 代碼如下:
exec sp_executesql N'update [dbo].[Region]set [RegionDescription] = @0where ([RegionID] = @1)', N'@0 nchar(4),@1 int', @0 = N'因?yàn)闇y(cè)試', @1 = 4
使用該方法,可以將不再當(dāng)前連接集合中的實(shí)體使用Attach方法加入到當(dāng)前集合中 在使用 SetModifiedProperty 來設(shè)置修改字段,使用該方法不必再執(zhí)行查詢將數(shù)據(jù)讀入當(dāng)前連接對(duì)象才能修改
您可能感興趣的文章:- ADO.NET 連接數(shù)據(jù)庫(kù)字符串小結(jié)(Oracle、SqlServer、Access、ODBC)
- ADO.NET實(shí)體數(shù)據(jù)模型詳細(xì)介紹
- ADO.NET中的五個(gè)主要對(duì)象的詳細(xì)介紹與應(yīng)用
- ADO.Net 類型化DataSet的簡(jiǎn)單介紹
- ADO.NET之連接池技術(shù)的使用詳解
- ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對(duì)象
- ADO.NET編程之基礎(chǔ)知識(shí)
- ADO.NET基礎(chǔ)知識(shí)匯總