コード例 #1
0
 public async Task AddOrEditAnimalAsync(Animal animal)
 {
     //Check if animal has got an Id, and if so update it.
     if (animal.AnimalId != 0)
     {
         //Check if animal exists loacally, and if it is detach it, so can update it.
         Animal local = petShop.Set <Animal>().Local.FirstOrDefault(entry => entry.AnimalId.Equals(animal.AnimalId));
         if (local != null)
         {
             // detach
             petShop.Entry(local).State = EntityState.Detached;
         }
         // set Modified flag in your entry
         petShop.Entry(animal).State = EntityState.Modified;
     }
     else
     {
         await petShop.Animals.AddAsync(animal);;
     }
     await petShop.SaveChangesAsync();
 }