Example #1
0
        public static void UndoDbEntries <T>(this DbContextWithTriggers context) where T : class
        {
            if (context == null)
            {
                throw new ArgumentNullException();
            }

            foreach (var entry in context.ChangeTracker.Entries <T>())
            {
                switch (entry.State)
                {
                case EntityState.Added: {
                    entry.State = EntityState.Detached;
                    break;
                }

                case EntityState.Deleted: {
                    entry.Reload();
                    break;
                }

                case EntityState.Modified: {
                    entry.State = EntityState.Unchanged;
                    break;
                }

                default: break;
                }
            }
        }
Example #2
0
        public static void UndoDbEntry(this DbContextWithTriggers context, object entity)
        {
            if (context == null || entity == null)
            {
                throw new ArgumentNullException();
            }
            var entry = context.Entry(entity);

            switch (entry.State)
            {
            case EntityState.Added: {
                entry.State = EntityState.Detached;
                break;
            }

            case EntityState.Deleted: {
                entry.Reload();
                break;
            }

            case EntityState.Modified: {
                entry.State = EntityState.Unchanged;
                break;
            }

            default: break;
            }
        }
Example #3
0
        public static void UndoDbEntityProperty(this DbContextWithTriggers context, object entity, string propertyName)
        {
            if (context == null || entity == null || propertyName == null)
            {
                throw new ArgumentException();
            }

            try {
                DbEntityEntry entry = context.Entry(entity);
                if (entry.State == EntityState.Added || entry.State == EntityState.Detached)
                {
                    return;
                }

                // Get and Set the Property value by the Property Name.
                object propertyValue = entry.OriginalValues.GetValue <object>(propertyName);
                entry.Property(propertyName).CurrentValue = entry.Property(propertyName).OriginalValue;
            } catch {
                throw;
            }
        }