static void UpdateDonator(Donator donator) { using (var db = new Context.Context()) { db.Entry(donator).State = EntityState.Modified; db.SaveChanges(); } }
static void UpdateDonatorEnhanced(Donator originalDonator, Donator newDonator) { using (var db = new Context.Context()) { //从数据库中检索最新的模型 var donator = db.Donators.Find(originalDonator.Id); //接下来检查用户修改的每个属性 if (originalDonator.Name != newDonator.Name) { //将新值更新到数据库 donator.Name = newDonator.Name; } if (originalDonator.Amount != newDonator.Amount) { //将新值更新到数据库 donator.Amount = newDonator.Amount; } //这里省略其他属性... db.SaveChanges(); } }