Exemplo n.º 1
0
        public virtual async Task SaveAsync(IntranetDbContext context = null)
        {
            bool isExternalContext = context != null;

            context = context ?? DbContextAsync;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                // for cases when we trying to update entities which does not present in database.
                var entries = context.ChangeTracker.Entries()
                              .Where(i => i.State == EntityState.Modified);
                foreach (var entry in entries)
                {
                    var dbValues = await entry.GetDatabaseValuesAsync();

                    if (dbValues == null)
                    {
                        entry.State = EntityState.Added;
                    }
                }

                await context.SaveChangesAsync();
            }
            finally
            {
                if (!isExternalContext)
                {
                    context.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed && disposing)
            {
                _dbContext.Dispose();
            }

            _disposed = true;
        }
Exemplo n.º 3
0
        public async Task DeleteAsync(IEnumerable <T> entities, IntranetDbContext context = null)
        {
            var isExternalContext = context != null;

            context = context ?? DbContextAsync;

            var set = context.Set <T>();

            var entityList = entities.ToList();

            entityList.ForEach(e => set.Attach(e));

            set.RemoveRange(entityList);

            await SaveAsync(context);

            if (!isExternalContext)
            {
                context.Dispose();
            }
        }