示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }