Пример #1
0
        /// <summary>
        /// Update database with entity and filter
        /// Ex: Update(obj, ob=>ob.objId == id)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public Boolean Update <T>(ObjectContext context, T entity, Expression <Func <T, bool> > filter, out OperationResult operationResult) where T : class, new()
        {
            try
            {
                if (filter == null)
                {
                    operationResult = new OperationResult {
                        Type = OperationResult.ResultType.Warning
                    };
                    return(false);
                }
                using (context)
                {
                    operationResult = new OperationResult {
                        Type = OperationResult.ResultType.Success
                    };

                    T entityFromDb = context.CreateObjectSet <T>().Where(filter).Single();
                    if (entityFromDb == null)
                    {
                        throw new NullReferenceException();
                    }

                    DataUtility.CloneObject <T>(entity, ref entityFromDb);

                    context.SaveChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                operationResult = new OperationResult {
                    Type = OperationResult.ResultType.Failure, Message = ex.StackTrace
                };
                return(false);
            }
        }