Exemplo n.º 1
0
 static void UpdateDonator(Donator donator)
 {
     using (var db = new Context.Context())
     {
         db.Entry(donator).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
Exemplo n.º 2
0
        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();
            }
        }