public static void UpdateDonator(Donator donator) { using (var db = new Context()) { db.Entry(donator).State = EntityState.Modified; db.SaveChanges(); } }
public static void UpdateDonatorEnhanced(Donator originalDonator, Donator newDonator) { using (var db = new 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(); }; }
public static void UpdateDonatorEnhanced(Donator originalDonator,Donator newDonator) { using(var db=new 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(); }; }