Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple columns in Entity Framework

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();

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

like image 530
Ahmed Shamel Avatar asked Jul 14 '26 06:07

Ahmed Shamel


1 Answers

you can Use EntityState Like this:

var user=db.users.Find(userId);
user.name="new name";
user.age=txtAge.text;
user.address=txtAddress.text;
context.Entry(user).State=Entitystate.Modified;
like image 118
Uthman Rahimi Avatar answered Jul 18 '26 02:07

Uthman Rahimi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!