更新实体框架中的多个列

Ahm*_*mel 3 c# asp.net-mvc entity-framework

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

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();
Run Code Online (Sandbox Code Playgroud)

db.Entry(user).Property有没有更好的方法来更新列而不需要多次重复代码?

Uth*_*imi 5

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;
Run Code Online (Sandbox Code Playgroud)