Exemplo n.º 1
0
        public void Update(Commande commande)
        {
            using (var context = new MyPlaceContext())
            {
                using (var dbContextTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        HashSet <int> selected = new HashSet <int>();
                        if (commande.DetailCommande == null)
                        {
                            commande.DetailCommande = new HashSet <DetailCommande>();
                        }
                        else
                        {
                            foreach (DetailCommande item in commande.DetailCommande)
                            {
                                selected.Add(item.ProduitId);
                                if (item.Quantite <= 0)
                                {
                                    context.Entry(item).State = EntityState.Deleted;
                                }
                                else if (item.CommandeId != commande.Id)
                                {
                                    item.CommandeId           = commande.Id;
                                    context.Entry(item).State = EntityState.Added;
                                }
                                else
                                {
                                    context.Entry(item).State = EntityState.Modified;
                                }
                            }
                        }
                        var old = context.detailCommande.Where(dc => dc.CommandeId == commande.Id && !selected.Contains(dc.ProduitId)).ToList();
                        old.ForEach(x => context.Entry(x).State = EntityState.Deleted);
                        context.Entry(commande).State           = EntityState.Modified;
                        context.SaveChanges();

                        dbContextTransaction.Commit();
                    }
                    catch
                    {
                        dbContextTransaction.Rollback();
                        foreach (DetailCommande item in commande.DetailCommande)
                        {
                            context.Entry(item).Reference(dc => dc.Produit).Load();
                        }
                        throw;
                    }
                }
            }
        }
Exemplo n.º 2
0
 public void Update(Categorie categorie)
 {
     using (var context = new MyPlaceContext())
     {
         using (var dbContextTransaction = context.Database.BeginTransaction())
         {
             try
             {
                 context.Entry(categorie).State = EntityState.Modified;
                 context.SaveChanges();
                 dbContextTransaction.Commit();
             }
             catch
             {
                 dbContextTransaction.Rollback();
                 throw;
             }
         }
     }
 }