如何只更新修改的值(EntityFramework 5.0)?

编程入门 行业动态 更新时间:2024-10-23 01:32:59
本文介绍了如何只更新修改的值(EntityFramework 5.0)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个实体,想要使用entityframework更新

I have this entity, want to update using entityframework

EmployeeModel employee = new EmployeeModel { Id = 1000, //This one must FirstName = modifiedValue, Email = modifiedValue, LastName = originalValue, Phone = originalValue };

要更新的代码

_db.ObjectStateManager.ChangeObjectState(employee, EntityState.Modified); _db.SaveChanges();

这是一次更新的SQL语句

This is the SQL statement got once updated

Update Employee set Id=1138,FirstName='modifiedValue',Email='modifiedValue',LastName= 'OriginalValue',phone='originalValue' where Id=1138

但我期待这个

Update Employee set FirstName='modifiedValue', Email='modifiedValue' where Id=1138.

我不知道我在这里失踪了。请告诉我。

I dont know what I am missing here. Please let me know.

推荐答案

这是我得到的解决方案

var entity = _db.CreateObjectSet<Employee>(); entity.Detach(employee); entity.Attach(employee); foreach (string modifiedPro in employeeModel.ModifiedProperties){ _db.ObjectStateManager.GetObjectStateEntry(employee).SetModifiedProperty(modifiedPro);} _db.SaveChanges();

仅在sql update语句中修改的值

Only modified values in the sql update statement

Update Employee set FirstName='modifiedValue', Email='modifiedValue' where Id=1138.

如果任何人都知道更好的答案,请张贴你的建议

If anybody knows better answer than this, Please post your suggestions

更多推荐

如何只更新修改的值(EntityFramework 5.0)?

本文发布于:2023-11-28 13:03:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:EntityFramework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!