数据库教程:ADO.NET EF中的实体修改方法

1.传统修改模式,看下列代码 复制代码 代码如下: using (northwindentities context = new northwindentities())

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 来设置修改字段,使用该方法不必再执行查询将数据读入当前连接对象才能修改

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/618702.html

(0)
上一篇 2021年5月21日
下一篇 2021年5月21日

精彩推荐