public async Task <PartProducer> CreateProducerIfNotExist(string producerName) { var findByName = this._dbContext.PartProducers.FirstOrDefault(x => x.Name == producerName); if (findByName == null) { findByName = new PartProducer { Name = producerName, }; await this._dbContext.AddAsync(findByName); await this._dbContext.SaveChangesAsync(); } return(findByName); }
public async Task <bool> AddRangeForProducer(IEnumerable <ProducerCatalogItem> items, PartProducer producer) { items = items.Where(x => !string.IsNullOrEmpty(x.ProducerCode)); this.SetTrimmedForItems(items); items = items.Where(x => !string.IsNullOrEmpty(x.ProducerCodeTrimmed)) .GroupBy(x => x.ProducerCodeTrimmed).Select(x => x.FirstOrDefault()) .Where(x => x != null && x.ProducerCode != null && x.Producer != null).ToList(); var articlesTrimmed = items.Select(x => x.ProducerCode).ToList(); var oldItems = this._dbContext.ProducerCatalogItems.Where(x => x.ProducerId == producer.ID && articlesTrimmed.Contains(x.ProducerCodeTrimmed)); this._dbContext.ProducerCatalogItems.RemoveRange(oldItems); await this._dbContext.SaveChangesAsync(); await this._dbContext.ProducerCatalogItems.AddRangeAsync(items); await this._dbContext.SaveChangesAsync(); return(true); }