且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用实体框架 6 更新记录?

更新时间:2023-12-01 17:59:28

您正在尝试更新记录(对我而言,这意味着更改现有记录上的值并将其保存回来").因此,您需要检索对象、进行更改并保存.

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