Exemplo n.º 1
0
 private void PreApplyWraps(TModel entity, EntityWrap <TModel> wrap)
 {
     foreach (var dels in wrap.CollectionList)
     {
         TrackCollection(entity, dels);
     }
     foreach (var entry in _context.ChangeTracker.Entries())
     {
         if (entry.State != EntityState.Deleted)
         {
             entry.State = EntityState.Detached;
         }
     }
 }
Exemplo n.º 2
0
        private void PostApplyWraps(TModel entity, EntityWrap <TModel> wrap)
        {
            // return;
            foreach (var entry in _context.ChangeTracker.Entries())
            {
                if (wrap.ExclusionList.Contains(entry.Entity.GetType()))
                {
                    entry.State = EntityState.Unchanged;

                    //_context.Entry(entry.Entity).State = EntityState.Detached;
                }
                if (wrap.CompanyList.Contains(entry.Entity.GetType()))
                {
                    AssignCompantAttr(entry.Entity as TModel);
                }
            }
        }
Exemplo n.º 3
0
        public virtual async Task <bool> UpdateEntityAsync(TModel entity, EntityWrap <TModel> wrap)
        {
            var res = false;

            try
            {
                if (wrap != null)
                {
                    PreApplyWraps(entity, wrap);
                }
                AssignCompantAttr(entity);
                _context.Update(entity);
                var entry = _context.Entry(entity);
                if (entry.State == EntityState.Modified)
                {
                    if (entry.OriginalValues.GetValue <int>("HotelId") != this.CompanyId)  //something wrong with hack
                    {
                        throw new Exception("Fobidden");
                    }
                }
                if (wrap != null)
                {
                    PostApplyWraps(entity, wrap);
                }
                await _context.SaveChangesAsync();

                res = await PostUpdateEntityAsync(entity);
            }
            catch (DbUpdateConcurrencyException exdb)
            {
                _logger.LogError(exdb, "Update {0}", entity.GetType().Name);
                if (_context.Find(entity.GetType(), entity.Id) == null)
                {
                    return(false);
                }
                return(false);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Update {0}", entity.GetType().Name);
                return(false); //to do
            }
            return(res);
        }