public virtual async Task <TEntity> AddAsync(TEntity entityToAdd, CancellationToken cancellationToken) { if (entityToAdd == null) { throw new ArgumentNullException(nameof(entityToAdd)); } IEntityWithDefaultGuidKey entityToAddAsEntityWithDefaultGuidKey = entityToAdd as IEntityWithDefaultGuidKey; if (entityToAddAsEntityWithDefaultGuidKey != null && entityToAddAsEntityWithDefaultGuidKey.Id == Guid.Empty) { entityToAddAsEntityWithDefaultGuidKey.Id = Guid.NewGuid(); } _set.Add(entityToAdd); await SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(entityToAdd); }
public virtual TEntity Add(TEntity entityToAdd) { if (entityToAdd == null) { throw new ArgumentNullException(nameof(entityToAdd)); } IEntityWithDefaultGuidKey entityToAddAsEntityWithDefaultGuidKey = entityToAdd as IEntityWithDefaultGuidKey; if (entityToAddAsEntityWithDefaultGuidKey != null && entityToAddAsEntityWithDefaultGuidKey.Id == Guid.Empty) { entityToAddAsEntityWithDefaultGuidKey.Id = Guid.NewGuid(); } _set.Add(entityToAdd); SaveChanges(); return(entityToAdd); }
public virtual IEnumerable <TEntity> AddRange(IEnumerable <TEntity> entitiesToAdd) { if (entitiesToAdd == null) { throw new ArgumentNullException(nameof(entitiesToAdd)); } foreach (IEntity entityToAdd in entitiesToAdd) { IEntityWithDefaultGuidKey entityToAddAsEntityWithDefaultGuidKey = entityToAdd as IEntityWithDefaultGuidKey; if (entityToAddAsEntityWithDefaultGuidKey != null && entityToAddAsEntityWithDefaultGuidKey.Id == Guid.Empty) { entityToAddAsEntityWithDefaultGuidKey.Id = Guid.NewGuid(); } } _set.AddRange(entitiesToAdd); SaveChanges(); return(entitiesToAdd); }
public virtual async Task <IEnumerable <TEntity> > AddRangeAsync(IEnumerable <TEntity> entitiesToAdd, CancellationToken cancellationToken) { if (entitiesToAdd == null) { throw new ArgumentNullException(nameof(entitiesToAdd)); } foreach (IEntity entityToAdd in entitiesToAdd) { IEntityWithDefaultGuidKey entityToAddAsEntityWithDefaultGuidKey = entityToAdd as IEntityWithDefaultGuidKey; if (entityToAddAsEntityWithDefaultGuidKey != null && entityToAddAsEntityWithDefaultGuidKey.Id == Guid.Empty) { entityToAddAsEntityWithDefaultGuidKey.Id = Guid.NewGuid(); } } _dbContext.AddRange(entitiesToAdd); await SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(entitiesToAdd); }