繁体   English   中英

实体框架DbContext更新值无需查询和外键

[英]Entity framework DbContext update value without query and by foreign key

我有一个更新一些表的方法。 对于更新,我需要首先获得TestProcess ,但我不喜欢它。 如何在没有select(firstOrDefault)操作的情况下更新TestProcess ,仅用于更新操作?

方法示例:

public void UpdateTestProcess(int id, string updateID)
{
    using (TestEntities context = new TestEntities())
                {
                    TestProcess pr = context.TestProcess.FirstOrDefault(x => x.MyID == id);
                    pr.UpdateID = updateID;             

                    context.TestProcess.Attach(pr);
                    context.ObjectStateManager.ChangeObjectState(pr, EntityState.Modified);
                    context.SaveChanges();
               }
}
TestProcess pr = new TestProcess()
{
    MyID == id,
};

context.Set<TestProcess>().Attach(pr);

pr.UpdateID = updateID;

context.SaveChanges();

如果要将值设置为该类型的默认值(例如,将int设置为0 ),则不会将其作为更改进行拾取,您需要手动设置状态。

pr.UpdateID = updateID;
context.Entry(pr).Property(p => p.UpdateID).IsModified = true;

你可以把这些代码放在扩展方法中,这样你就可以做这样的事情(我将把实现留作练习):

Foo entity = this.DbContext.GetEntityForUpdate<Foo>(
    item => item.ID, model.ID
    );

this.DbContext.UpdateProperty(entity, item => item.Name, model.Name);

您可以这样做(您可能应该拥有所有测试过程数据):

TestProcess pr = new TestProcess();

pr.Id = id;
pr.UpdateID = updateID;

context.Attach(pr);
context.ObjectStateManager.ChangeObjectState(pr, EntityState.Modified);
context.SaveChanges();

编码:

TestProcess testprocess = dbcontext.TestProcesses.Attach(new TestProcess { MyID = id });
tp.UpdateID = updateID;
dbcontext.Entry<TestProcess>(testprocess).Property(tp => tp.UpdateID).IsModified = true;
dbcontext.Configuration.ValidateOnSaveEnabled = false;
dbcontext.SaveChanges();

结果TSQL:

exec sp_executesql N'UPDATE [dbo].[TestProcesses]
SET [UpdateID] = @0
WHERE ([MyID] = @1)
',N'@0 bigint,@1 bigint',@0=2,@1=1

注意:

需要“IsModified = true”行,因为在创建新的TestProcess对象时(仅填充了MyID属性),所有其他属性都具有其默认值(0,null等)。 如果要使用“默认值”更新数据库,实体框架将不会检测到更改,然后不会更新数据库。

例如:

testprocess.UpdateID = null;

如果没有“IsModified = true”这一行将无法工作,因为属性UpdateID在创建空的TestProcess对象时已经为空,您需要向EF说这个列必须更新,这就是这一行的目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM