public void AddAgreement(int personId, AgreementDto agreementDto) { using (var context = new GdDbContext()) { GdDbConfiguration.SuspendExecutionStrategy = true; using (var dbContextTransaction = context.Database.BeginTransaction()) { try { context.Configuration.AutoDetectChangesEnabled = false; //var personAdminId = Context.Users.Single(x => x.Email == ConfigurationManager.AppSettings["DefaultGdAdminEmail"]).PersonId; // Dit werkt niet. Moet in 2 stappen var adminEmail = ConfigurationManager.AppSettings["DefaultGdAdminEmail"]; var personAdminId = context.Users.FirstOrDefaultAsync(x => x.Email == adminEmail).Result.PersonId; var personPM = context.Persons.First(x => x.Name == "My name"); var person = context.Persons.FirstOrDefault(el => el.Id == personId); if (person == null) { throw new GraphicsDetectiveInvalidDataTypeException($"No person found for Id: {personId}"); } if (agreementDto == null) { throw new GraphicsDetectiveInvalidDataTypeException("Invalid agreementDto"); } var agreement = new Agreement { Date = agreementDto.DateTime, AgreementType = AgreementType.WwwImageSearch, ImageSearchAppointments = new List() }; context.Agreements.Add(agreement); var personAgreementRelations = new List() { new PersonAgreementRelation { Agreement = agreement, Person = person, PersonAgreementRole = PersonAgreementRole.Client }, new PersonAgreementRelation { Agreement = agreement, PersonAgreementRole = PersonAgreementRole.Supplier, PersonId = personPM.Id }, new PersonAgreementRelation { Agreement = agreement, PersonAgreementRole = PersonAgreementRole.Admin, PersonId = personAdminId } }; context.PersonAgreementRelations.AddRange(personAgreementRelations); //TODO: Check if OKAY!!! if (agreementDto.ImageSearchAppointmentDto.Count == 0) { throw new GraphicsDetectiveInvalidDataTypeException( "Count of imagesearchappointments can't be lower than 0"); } var imageSearchAppointmentDto = agreementDto.ImageSearchAppointmentDto.First(); var imageSearchAppointment = new ImageSearchAppointment() { AgreementId = agreement.Id, HasImageFeed = false, Name = imageSearchAppointmentDto.Name, Periodicity = imageSearchAppointmentDto.Periodicity, PeriodicityCategory = imageSearchAppointmentDto.PeriodicityCategory, ShowResultsToCustomer = imageSearchAppointmentDto.ShowResultsToCustomer, ImageSearchCommands = new List() }; context.ImageSearchAppointments.Add(imageSearchAppointment); if (!imageSearchAppointment.ShowResultsToCustomer) { var imageSearchAppointmentWebDomainExtensions = imageSearchAppointmentDto .WebDomainExtensionDtos .Select(x => new ImageSearchAppointmentWebDomainExtension() { WebDomainExtensionId = x.Id, ImageSearchAppointment = imageSearchAppointment, ImageSearchAppointmentId = imageSearchAppointment.Id }) .ToList(); context.ImageSearchAppointmentWebDomainExtensions.AddRange( imageSearchAppointmentWebDomainExtensions); } var imageSearchCommandDto = imageSearchAppointmentDto.ImageSearchCommandDto.First(); var imageSearchCommand = new ImageSearchCommand() { ImageSearchAppointment = imageSearchAppointment, ImageSearchAppointmentId = imageSearchAppointment.Id, Date = imageSearchCommandDto.Date, NumberOfImages = imageSearchCommandDto.NumberOfImages, ImageCollectionProcessedDate = imageSearchCommandDto.ImageCollectionProcessedDate }; context.ImageSearchCommands.Add(imageSearchCommand); context.ChangeTracker.DetectChanges(); context.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception ex) { dbContextTransaction.Rollback(); } } GdDbConfiguration.SuspendExecutionStrategy = false; } } public static IEnumerable AddRange(this IDbSet dbSet, IEnumerable entitiesToAdd) where T : class { return ((DbSet) dbSet).AddRange(entitiesToAdd);