public virtual async Task UpdateWithSpecifiedColumnsOnlyAsync(T entity, params Expression <Func <T, object> >[] properties) { foreach (var property in properties) { _dbContext.Entry(entity).Property(property).IsModified = true; } try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException due) { _logger.LogError(due.Message, due.InnerException); throw; } }
public static async Task SetTestData(MatchingDbContext dbContext, Provider provider, ProviderVenue venue, Opportunity opportunity, BackgroundProcessHistory backgroundProcessHistory, bool isSaved = true, bool isSelectedForReferral = true) { backgroundProcessHistory.Status = BackgroundProcessHistoryStatus.Pending.ToString(); await dbContext.AddAsync(provider); await dbContext.AddAsync(venue); await dbContext.AddAsync(opportunity); await dbContext.AddAsync(backgroundProcessHistory); await dbContext.SaveChangesAsync(); dbContext.DetachAllEntities(); var items = dbContext.OpportunityItem .AsNoTracking() .Where(oi => oi.OpportunityId == opportunity.Id) .ToList(); foreach (var opportunityItem in items) { opportunityItem.IsSaved = isSaved; opportunityItem.IsCompleted = false; opportunityItem.OpportunityType = "Referral"; opportunityItem.IsSelectedForReferral = isSelectedForReferral; dbContext.Entry(opportunityItem).Property("IsSaved").IsModified = true; dbContext.Entry(opportunityItem).Property("IsCompleted").IsModified = true; dbContext.Entry(opportunityItem).Property("IsSelectedForReferral").IsModified = true; dbContext.Entry(opportunityItem).Property("OpportunityType").IsModified = true; } await dbContext.SaveChangesAsync(); dbContext.DetachAllEntities(); }
private static async Task SetProvisionGapData( MatchingDbContext dbContext, OpportunityItem opportunityItem, bool hasBadExperience, bool hasNoSuitableStudent, bool areProvidersTooFarAway) { opportunityItem.OpportunityType = "ProvisionGap"; opportunityItem.IsSaved = true; opportunityItem.IsCompleted = false; //Need to remove extra referral/provision gap rows created as AutoDomainData foreach (var x in opportunityItem.ProvisionGap.Where(pg => pg.Id != opportunityItem.ProvisionGap.First().Id).ToList()) { opportunityItem.ProvisionGap.Remove(x); } opportunityItem.Referral.Clear(); await dbContext.OpportunityItem.AddAsync(opportunityItem); await dbContext.SaveChangesAsync(); dbContext.DetachAllEntities(); //Set up the provision gap record var provisionGap = await dbContext.ProvisionGap.AsNoTracking().FirstOrDefaultAsync(); provisionGap.HadBadExperience = hasBadExperience; provisionGap.NoSuitableStudent = hasNoSuitableStudent; provisionGap.ProvidersTooFarAway = areProvidersTooFarAway; dbContext.Entry(provisionGap).Property("HadBadExperience").IsModified = true; dbContext.Entry(provisionGap).Property("NoSuitableStudent").IsModified = true; dbContext.Entry(provisionGap).Property("ProvidersTooFarAway").IsModified = true; await dbContext.SaveChangesAsync(); dbContext.DetachAllEntities(); }
public async Task Then_Make_TLevel_Provider_Flag_To_True( MatchingDbContext dbContext, Domain.Models.Provider provider, List <ProviderVenueViewModel> venueViewModels, MatchingConfiguration config, ILogger <GenericRepository <Domain.Models.Provider> > providerLogger, ILogger <GenericRepository <ProviderReference> > providerReferenceLogger, IHttpContextAccessor httpContextAccessor, IDateTimeProvider dateTimeProvider ) { //Arrange provider.IsTLevelProvider = false; await dbContext.AddAsync(provider); await dbContext.SaveChangesAsync(); dbContext.Entry(provider).State = EntityState.Detached; var viewModel = new ProviderDetailViewModel { Id = provider.Id, IsTLevelProvider = true, ProviderVenues = venueViewModels }; httpContextAccessor.HttpContext.Returns(new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.GivenName, "System") })) }); var mapperConfig = new MapperConfiguration(c => { c.AddMaps(typeof(ProviderMapper).Assembly); c.ConstructServicesUsing(type => type.Name.Contains("LoggedInUserNameResolver") ? (object)new LoggedInUserNameResolver <ProviderDetailViewModel, Domain.Models.Provider>(httpContextAccessor) : type.Name.Contains("UtcNowResolver") ? new UtcNowResolver <ProviderDetailViewModel, Domain.Models.Provider>(dateTimeProvider) : null); }); var mapper = new Mapper(mapperConfig); var providerReferenceRepo = new GenericRepository <ProviderReference>(providerReferenceLogger, dbContext); var repo = new GenericRepository <Domain.Models.Provider>(providerLogger, dbContext); var providerService = new ProviderService(mapper, repo, providerReferenceRepo); var sut = new ProviderController(providerService, config); //Act await sut.SaveProviderDetailAsync(viewModel); //Assert var result = await repo.GetSingleOrDefaultAsync(x => x.Id == provider.Id); provider.IsTLevelProvider.Should().BeFalse(); result.IsTLevelProvider.Should().BeTrue(); }