Home > Database > Mysql Tutorial > body text

ADO.NET EF中的实体修改方法

WBOY
Release: 2016-06-07 18:01:21
Original
864 people have browsed it

ADO.NET EF中的实体修改方法

1.传统修改模式,看下列代码
代码如下:
using (NorthwindEntities context = new NorthwindEntities())
{
Region region = context.Region.FirstOrDefault(v => v.RegionID == 4);
region.RegionDescription = "Test";
context.SaveChanges();
}



监控SQL语句:
代码如下:
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


从这里例子中可以看出使用“传统模式”的数据更新,必须先要执行一次查询,将获取要更新的实体对象,在看下面的例子:
代码如下:
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();
}



更新是不会执行的,因为实体不再 执行 SaveChanges 的对象中所以 当我们更新一个 不再当前连接中的对象是必须要先执行查询获取这个对象才能对其更新,如下:
代码如下:
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 修改实体
代码如下:
Region region;
using (NorthwindEntities ne = new NorthwindEntities())
{
//利用EntityObject.Execute(MergeOption.NoTracking),等效于使用ObjectContext.Dettach(EntityObject)
//查询并分离对象
region = ne.Region.Execute(MergeOption.NoTracking).Where(v => v.RegionID == 1).FirstOrDefault();
}
//修改分离的值
region.RegionDescription = "TestTest1";
//使用分离的对象 order 更新
using (NorthwindEntities context = new NorthwindEntities())
{
//将数据载入到context中以便更新
context.GetObjectByKey(region.EntityKey);
//使用order 更新 context中的对应对象
context.ApplyPropertyChanges(region.EntityKey.EntitySetName, region);
context.SaveChanges();
}

监控SQL语句:


代码如下:
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的解释是“将已分离对象的属性更改应用于已附加到对象上下文的对象。”其实说白了就是 拿旧对象去更新新对象,我们可以看出 使用“ApplyPropertyChanges 修改实体”方法修改实体与 使用“传统模式”是一样的,都是必须先执行一次查询,获取更新的对象,但是 ApplyPropertyChanges方法的特殊之处是,该方法会拿内存中的对象(新对象)和当前连接中的对象(旧对象)对比,自动生成对应字段修改的Update语句,如果内存中的对象与当前连接中的对象完全相等(每个字段的值都相等),将不生成响应的Update。当我们再次执行 上述代码观察监控到了SQL语句,你会发现只监控到SQL1,不会得到SQL2。

3.使用Attach与SetModifiedProperty修改实体
代码如下:
using (NorthwindEntities context = new NorthwindEntities())
{
Region region = context.Region.FirstOrDefault(v => v.RegionID == 4);
context.Detach(region);
region.RegionDescription = "因为测试";

context.Attach(region);
var newRegion = context.ObjectStateManager.GetObjectStateEntry(region);
newRegion.SetModified();
newRegion.SetModifiedProperty("RegionDescription");

context.SaveChanges();
}

监视SQL语句:

代码如下:
exec sp_executesql N'update [dbo].[Region]set [RegionDescription] = @0where ([RegionID] = @1)', N'@0 nchar(4),@1 int', @0 = N'因为测试', @1 = 4

使用该方法,可以将不再当前连接集合中的实体使用Attach方法加入到当前集合中 在使用 SetModifiedProperty 来设置修改字段,使用该方法不必再执行查询将数据读入当前连接对象才能修改
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template