更新实体框架中的多个列 [英] Update multiple columns in Entity Framework

查看:55
本文介绍了更新实体框架中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新实体框架中的多个列.我现在使用这个:

I want to update multiple columns in Entity Framework. I now use this :

var user = new Relations { Id=1, Status = 1, Date = DateTime.Now, Notification = 0 };

db.Relations.Attach(user);

db.Entry(user).Property(x => x.Status).IsModified = true;
db.Entry(user).Property(x => x.Notification).IsModified = true;
db.Entry(user).Property(x => x.Date).IsModified = true;

db.Configuration.ValidateOnSaveEnabled = false;
db.SaveChanges();

是否有更好的方法来更新列而不重复几次代码db.Entry(user).Property?

Is there a better way to update columns without repeating the code db.Entry(user).Property several times ?

推荐答案

您可以像这样使用EntityState:

var user=db.users.Find(userId);
user.name="new name";
user.age=txtAge.text;
user.address=txtAddress.text;
context.Entry(user).State=Entitystate.Modified;

这篇关于更新实体框架中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆