Exemplo n.º 1
0
        public virtual bool Update(TEntity updated, int key)
        {
            Debug.Assert(context != null);
            Debug.Assert(updated != null);

            try
            {
                //context.Entry<TEntity>(entity).State = EntityState.Modified;
                TEntity existing = context.Set <TEntity>().Find(key);
                //if (Debugger.IsAttached)
                //{
                //    Output.Write("Inside Repository for employeebio");
                //    Output.Write("existing model is: " + existing);
                //    Output.Write("updated model is: " + updated);
                //}
                if (existing != null)
                {
                    context.Entry(existing).CurrentValues.SetValues(updated);

                    //context.Set<TEntity>().Attach(updated);
                    //context.ObjectStateManager.ChangeObjectState(updated, EntityState.Modified);
                    //context.SaveChanges();
                }
                return(context.SaveChanges() > 0);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Output.Write("Entity of type \"" + eve.Entry.Entity.GetType().Name +
                                 "\"  in state \"" + eve.Entry.State + "\" has the following validation errors:");
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Output.WriteLine("- Property: \"" + ve.PropertyName + "\", Error: \"" + ve.ErrorMessage + "\"");
                    }
                }
                throw;
            }
            catch (Exception e)
            {
                Output.WriteLine("Error in data update : " + e);
                return(false);
            }
        }
Exemplo n.º 2
0
        public virtual bool Update(TEntity updated, int key)
        {
            Debug.Assert(context != null);
            Debug.Assert(updated != null);

            try
            {
                //context.Entry<TEntity>(entity).State = EntityState.Modified;
                TEntity existing = context.Set <TEntity>().Find(key);
                if (existing != null)
                {
                    context.Entry(existing).CurrentValues.SetValues(updated);
                }
                return(context.SaveChanges() > 0);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error in data update : " + e);
                return(false);
            }
        }