How to update record using Entity Framework 6?

You’re trying to update the record (which to me means “change a value on an existing record and save it back”). So you need to retrieve the object, make a change, and save it.

using (var db = new MyContextDB())
{
    var result = db.Books.SingleOrDefault(b => b.BookNumber == bookNumber);
    if (result != null)
    {
        result.SomeValue = "Some new value";
        db.SaveChanges();
    }
}

Leave a Comment