Esempio n. 1
0
 public void Delete(T entityToDelete)
 {
     if (_context.Entry(entityToDelete).State == EntityState.Detached)
     {
         _dbSet.Attach(entityToDelete);
     }
     _dbSet.Remove(entityToDelete);
 }
Esempio n. 2
0
        /// <summary>
        /// Save method.
        /// </summary>
        public void Save()
        {
            foreach (var entry in _context.ChangeTracker.Entries().Where(e => e.Entity is BaseEntity && (e.State == EntityState.Added || e.State == EntityState.Modified)))
            {
                if (entry.Entity is BaseEntity)
                {
                    BaseEntity baseEntity = entry.Entity as BaseEntity;

                    baseEntity.IsActive  = true;
                    baseEntity.IsDeleted = false;


                    //if (entry.Entity is DismissalCase)
                    //{
                    //    //baseEntity.Creator = baseEntity
                    //}

                    if (entry.State == EntityState.Added)
                    {
                        baseEntity.CreatedDate = DateTime.Now;
                        //baseEntity.Creator = 0; // ?
                    }
                    else
                    {
                        _context.Entry(baseEntity).Property(x => x.CreatedDate).IsModified = false;
                        _context.Entry(baseEntity).Property(x => x.Creator).IsModified     = false;
                    }
                    baseEntity.RowVersion = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 8).ToUpper() + new Random().Next(10000000, 20000000);
                    baseEntity.EditedDate = DateTime.Now;
                    //baseEntity.Editor = 0; // ?
                }
            }


            try
            {
                //_context.SaveChanges();
                SaveChanges(_context);
            }
            catch (DbEntityValidationException e)
            {
                var outputLines = new List <string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format("{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage));
                    }
                }
                //System.IO.File.AppendAllLines(@"C:\errors.txt", outputLines);

                throw e;
            }
        }