Ejemplo n.º 1
0
        public bool Update(List <T> dtoList)
        {
            using (var db = new OracleContext())
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    T entity = null;
                    try
                    {
                        var dbSet = db.Set <T>();
                        dtoList.ForEach(x =>
                        {
                            entity = dbSet.Find(x.ID);
                            if (entity != null)
                            {
                                db.Entry(entity).CurrentValues.SetValues(x);
                            }
                        });

                        db.SaveChanges();
                        trans.Commit();//Data Saved Successfully. Transaction Commited
                        return(true);
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Logger.Error(string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
                            }
                        }
                        return(false);
                    }
                    catch (DbException ex)
                    {
                        Logger.Error(ex.Message, ex);
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();//Error Occured during data saved. Transaction Rolled Back
                        Logger.Error(ex.Message, ex);
                        return(false);
                    }
                }
            }
        }
        private void BtnSalvar_Click(object sender, EventArgs e)
        {
            var cliente = new Cliente()
            {
                Nome = txtNome.Text
            };
            var db = new OracleContext();

            if (txtId.Text == String.Empty)
            {
                db.Clientes.Add(cliente);
            }
            else
            {
                cliente.Id = int.Parse(txtId.Text);
                db.Entry(cliente).State = EntityState.Modified;
            }
            db.SaveChanges();
            MessageBox.Show("Cliente salvo com sucesso");
            Close();
        }
Ejemplo n.º 3
0
        public T Update(T dto)
        {
            T entity = null;

            try
            {
                using (var db = new OracleContext())
                {
                    entity = db.Set <T>().Find(dto.ID);
                    if (entity != null)
                    {
                        db.Entry(entity).CurrentValues.SetValues(dto);
                        db.SaveChanges();
                    }
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Logger.Error(string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
                    }
                }
                entity = null;
            }
            catch (DbException ex)
            {
                Logger.Error(ex.Message, ex);
                entity = null;
            }
            catch (Exception ex)
            {
                entity = null;
                Logger.Error(ex.Message, ex);
            }

            return(entity);
        }