Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating database entry MVC Entity Framework

I have a Payment model with a 'Status' boolean value which defaults to false. Once payment has been made, I need to update that specific payment's 'Status' to true.

Here's the code I've been trying to use to change the specific database entry, but it's just not changing it. What am I doing wrong?

Payment payment = new Payment();
payment = db.Payments.Find(orderId);
db.Entry(payment).State = EntityState.Modified;
payment.Status = true;
db.SaveChanges();

Thanks!

This is what ended up working:

using (var con = new ApplicationDbContext())
{
    payment = con.Payments.First(x => x.Id == orderId);
    payment.Status = true;

    con.Payments.Attach(payment);
    var entry = con.Entry(payment);
    entry.Property(e => e.Status).IsModified = true;
    con.SaveChanges();
}
like image 468
Pierre Avatar asked Sep 09 '14 19:09

Pierre


1 Answers

Payment payment = new Payment();
payment = db.Payments.Find(orderId);
payment.Status = true;
db.Entry(payment).State = EntityState.Modified;
db.SaveChanges();
like image 51
ali golshani Avatar answered Oct 15 '22 04:10

ali golshani