Exemple #1
0
        /// <summary>
        /// Method responsible for select a lsit of member in database
        /// </summary>
        /// <typeparam name="T">Entity type</typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public List <T> Select <T>(Func <T, bool> where) where T : class
        {
            try
            {
                using (var context = new eManageContext())
                {
                    List <T> list;

                    IQueryable <T> dbQuery = context.Set <T>();

                    list = dbQuery
                           .AsNoTracking()
                           .Where(where)
                           .ToList <T>();

                    return(list);
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Exemple #2
0
        /// <summary>
        /// Method responsible for changing a member in database
        /// </summary>
        /// <typeparam name="T">Entity type</typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ProcResult Update <T>(T entity, int id = 0, params Expression <Func <T, object> >[] updatedProperties) where T : class
        {
            ProcResult proc = new ProcResult();

            proc.Success = true;
            proc.Message = Messages.ExecuteSuccess.ToString();

            using (var context = new eManageContext())
            {
                if (id == 0 || updatedProperties == null || !updatedProperties.Any())
                {
                    try
                    {
                        context.Entry(entity).State = EntityState.Modified;
                        context.SaveChanges();

                        proc.objResult = entity;
                    }
                    catch (Exception ex)
                    {
                        proc.Success = false;
                        proc.Message = ex.Message.ToString();
                    }
                }
                else
                {
                    var newObj = context.Set(entity.GetType()).Find(id);

                    var entry = context.Entry(newObj);


                    foreach (var property in entry.CurrentValues.PropertyNames)
                    {
                        context.Entry <T>((T)newObj).Property(property).IsModified = false;
                    }

                    foreach (var property in updatedProperties)
                    {
                        context.Entry <T>((T)newObj).Property(property).IsModified   = true;
                        context.Entry <T>((T)newObj).Property(property).CurrentValue = context.Entry <T>(entity).Property(property).CurrentValue;
                    }

                    try
                    {
                        context.SaveChanges();
                        proc.objResult = entity;
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        proc.Success = false;
                        proc.Message = ex.Message.ToString();
                    }
                    catch (Exception ex)
                    {
                        proc.Success = false;
                        proc.Message = ex.Message.ToString();
                    }
                }
            }

            return(proc);
        }