public virtual async Task <EventSettings <long> > Select(long eventSettingsId) { EventSettingsLong eventSettings = null; using (SenderDbContext context = _dbContextFactory.GetDbContext()) { eventSettings = await context.EventSettings .Include(x => x.TemplatesNavigation) .FirstOrDefaultAsync(x => x.EventSettingsId == eventSettingsId) .ConfigureAwait(false); } return(eventSettings); }
public void then_event_settings_updated_are_found_using_ef() { List <EventSettingsLong> actual = DbContext.EventSettings .Where(x => x.EventKey == _categoryId) .OrderBy(x => x.EventSettingsId) .ToList(); actual.Should().NotBeEmpty(); actual.Count.Should().Be(_insertedSettings.Count); for (int i = 0; i < _insertedSettings.Count; i++) { EventSettings <long> expectedItem = _insertedSettings[i]; EventSettingsLong actualItem = actual[i]; expectedItem.EventSettingsId = actualItem.EventSettingsId; expectedItem.Templates = null; actualItem.Should().BeEquivalentTo(expectedItem); } }
//insert public virtual async Task Insert(List <EventSettings <long> > items) { List <EventSettingsLong> mappedList = items .Select(_mapper.Map <EventSettingsLong>) .ToList(); using (Repository repository = new Repository(_dbContextFactory.GetDbContext())) using (IDbContextTransaction ts = repository.Context.Database.BeginTransaction()) { SqlTransaction underlyingTransaction = (SqlTransaction)ts.GetDbTransaction(); MergeCommand <EventSettingsLong> mergeCommand = repository.Merge(mappedList, underlyingTransaction); mergeCommand.Insert .ExcludeProperty(x => x.EventSettingsId) .ExcludeProperty(x => x.TemplatesNavigation); mergeCommand.Output .IncludeProperty(x => x.EventSettingsId); int changes = await mergeCommand.ExecuteAsync(MergeType.Insert).ConfigureAwait(false); for (int i = 0; i < mappedList.Count; i++) { EventSettingsLong mappedItem = mappedList[i]; items[i].EventSettingsId = mappedItem.EventSettingsId; if (mappedItem.Templates != null) { mappedItem.Templates.ForEach( x => x.EventSettingsId = mappedItem.EventSettingsId); } } List <DispatchTemplate <long> > templates = items .SelectMany(x => x.Templates) .ToList(); await InsertTemplates(templates, repository.Context, underlyingTransaction) .ConfigureAwait(false); ts.Commit(); } }