Exemplo n.º 1
0
        public async Task <TEntity> Add(TEntity entity)
        {
            try
            {
                dbContext.Add(entity);
                await dbContext.SaveChangesAsync();

                return(entity);
            }
            catch (Exception ex)
            {
                throw new Exception($"Couldn't add entity: {ex.Message}");
            }
        }
Exemplo n.º 2
0
        private async Task <bool> SaveChangesAsync()
        {
            if (!_dbContext.ChangeTracker.HasChanges())
            {
                return(true);
            }

            int count = 0;

            try
            {
                count = await _dbContext.SaveChangesAsync();
            }
            catch (Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException ex)
            {
                if (!string.IsNullOrEmpty(ex.Message) &&
                    (ex.Message.StartsWith("Database operation expected to affect 1 row(s)") ||                  // This one comes from SQL server
                     ex.Message == "Attempted to update or delete an entity that does not exist in the store.")) // This one comes from EF in-memory db
                {
                    return(false);
                }

                throw;
            }

            return(count > 0);
        }