public void SaveChangesLog(TEntity entity, Login user, Enums.MenuList menuId)
        {
            if (user != null)
            {
                var isAdded    = _context.ChangeTracker.Entries().Any(x => x.State == EntityState.Added);
                var isModified = _context.ChangeTracker.Entries().Any(x => x.State == EntityState.Modified);
                var isDeleted  = _context.ChangeTracker.Entries().Any(x => x.State == EntityState.Deleted);

                var action = "";
                if (isAdded)
                {
                    action = "Create";
                }

                if (isModified)
                {
                    action = "Modified";
                }
                _context.SaveChanges();
                ObjectContext        objectContext = ((IObjectContextAdapter)_context).ObjectContext;
                ObjectSet <TEntity>  set           = objectContext.CreateObjectSet <TEntity>();
                IEnumerable <string> keyNames      = set.EntitySet.ElementType
                                                     .KeyMembers
                                                     .Select(k => k.Name);
                string id = "";
                if (keyNames.Count() > 0)
                {
                    foreach (var name in keyNames)
                    {
                        id += _context.Entry(entity).CurrentValues[name].ToString();
                    }
                }

                _context.CHANGES_HISTORY.Add(new CHANGES_HISTORY()
                {
                    MODUL_ID      = (int)menuId,
                    FORM_ID       = long.Parse(id),
                    MODIFIED_BY   = user.USER_ID,
                    MODIFIED_DATE = DateTime.Now,
                    ACTION        = action
                });
                _context.SaveChanges();
            }
        }
Esempio n. 2
0
 public void SaveChanges()
 {
     try
     {
         _context.SaveChanges();
     }
     catch (DbEntityValidationException)
     {
         throw;
     }
     catch (Exception)
     {
         throw;
     }
 }