static model.PersonAddress MergePersonAddress(model.Person person, model.Address address, DateTime date) { Logger.generatorMergePA(); try { var personAddress = new model.PersonAddress(); if (address.City != null) { personAddress.Person = person; personAddress.Address = address; if (RandomNumber.Draw(1, 10) < 8) { personAddress.Assigned = person.BirthDate; } else { personAddress.Assigned = Date.getDate(person.BirthDate); } } return(personAddress); } catch (Exception e) { Logger.log(e); return(null); } }
public static void Generate(PeopleContext context, int?amount = null) { bool existsFlag = false; Logger.generatorMain(); int i = 0; try { while (true) { i++; Logger.start(); DateTime startTime = new DateTime(2020, 10, 1); var date = Date.getDate(startTime); Thread.Sleep(2000); #region PERSON GeneratorsClass.Person localPeroson = new GeneratorsClass.Person(); var person = PersonToPerson(localPeroson); context.People.Add(person); context.SaveChanges(); #endregion #region IS_DEAD if (localPeroson.IsDead == true) { var deceasedPerson = isDead(person, localPeroson); context.DeceasedPeople.Add(deceasedPerson); context.SaveChanges(); continue; } #endregion #region CONCTACT int inc = 1; while (true) { if (DateTime.Today.Year - person.BirthDate.Year < 65) { existsFlag = false; var contact = SetContact(person.FirstName, person.LastName); try { var test = context.Contacts.Where(e => e.Email == contact.Email).Where(p => p.PhoneNumber == contact.PhoneNumber).Count(); if (test > 0) { existsFlag = true; throw new DbUpdateException(); } context.Contacts.Add(contact); context.SaveChanges(); var personContact = MergePersonContact(person, contact); contact.PersonContacts.Add(personContact); } catch (DbUpdateException) { Logger.generatorContactDbUpdateError(); break; } if ((existsFlag || RandomNumber.Draw(1, (int)Math.Pow(100, inc)) == 5) && context.Contacts.Count() > 2) { context.PersonContacts.Add(MergePersonContact(person, context.Contacts.Skip(RandomNumber.Draw(0, context.Contacts.Count() - 2)).First())); } } else { if (RandomNumber.Draw(1, 10) == 5 && context.Contacts.Count() > 2) { var drawContact = context.Contacts.Skip(RandomNumber.Draw(0, context.Contacts.Count() - 2)).First(); if (context.PersonContacts.Where(p => p.PersonId == person.Id).Where(c => c.ContactId == drawContact.Id).Count() == 0) { context.PersonContacts.Add(MergePersonContact(person, drawContact)); } else { Logger.generatorExistsPC(); } } } context.SaveChanges(); if (RandomNumber.Draw(1, (int)Math.Pow(2, inc)) > 1) { break; } else { inc++; Logger.generatorOneMoreContact(); } } #endregion #region ADDRESS var address = SetAddress(localPeroson.Street); model.PersonAddress personAddress = new model.PersonAddress(); if (address.Street != "null" && address.City != "null") { context.SaveChanges(); try { existsFlag = false; var test = context.Addresses.Where(s => s.Street == address.Street).Where(c => c.City == address.City).Count(); if (test > 0) { existsFlag = true; throw new DbUpdateException(); } context.Addresses.Add(address); context.SaveChanges(); personAddress = MergePersonAddress(person, address, date); } catch (DbUpdateException) { Logger.generatorAddressDbUpdateError(); } } else if (context.Addresses.Select(x => x.Id).Count() > 2) { existsFlag = false; personAddress = MergePersonAddress(person, context.Addresses.Skip(RandomNumber.Draw(0, context.Addresses.Count() - 2)).First(), date); if (context.PersonAddresses.Where(x => x.AddressId == personAddress.AddressId).Where(x => x.PersonId == personAddress.PersonId).Count() > 0) { existsFlag = true; Logger.generatorExistsPA(); } } else { var deceasedPerson = isDead(person, localPeroson); context.DeceasedPeople.Add(deceasedPerson); context.SaveChanges(); continue; } if (!existsFlag) { context.PersonAddresses.Add(personAddress); context.SaveChanges(); } if (RandomNumber.Draw(1, 5) == 5 && context.Addresses.Count() > 2) { var drawAddress = context.Addresses.Skip(RandomNumber.Draw(0, context.Addresses.Count() - 2)).First(); if (context.PersonAddresses.Where(x => x.AddressId == personAddress.AddressId).Where(x => x.PersonId == personAddress.PersonId).Count() == 0) { context.PersonAddresses.Add(MergePersonAddress(person, drawAddress, date)); } else { Logger.generatorExistsPA(); } } #endregion #region END if (amount != null && amount <= i) { break; } #endregion context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); } context.SaveChanges(); }