private static void CreatePersonWithAddress() { using (var dbContext = new PersonDbContext()) { var person = new Person { FirstName = "Karel", LastName = "Horak", BirthDate = DateTime.Now.AddYears(-22) }; var address1 = new Address { City = "Ostrava", Number = "20", PostalCode = "45678", Country = "Czech", AddressType = AddressType.Home }; person.Addresses = new List <Address> { address1 }; dbContext.Persons.Add(person); dbContext.SaveChanges(); } }
private static void DeletePersonAddress(Func <Person, bool> singlePersonPredicate, Func <Address, bool> singleAddressPredicate) { using (var dbContext = new PersonDbContext()) { var person = dbContext.Persons .Include(nameof(Person.Addresses)) .Single(singlePersonPredicate); var addressToDelete = person.Addresses.Single(singleAddressPredicate); person.Addresses.Remove(addressToDelete); dbContext.SaveChanges(); } }
private static void UpdatePerson(Person personsWithAddress) { if (personsWithAddress == null) { return; } using (var dbContext = new PersonDbContext()) { personsWithAddress.BirthDate = DateTime.Today.AddYears(-30); dbContext.Persons.Attach(personsWithAddress); dbContext.Entry(personsWithAddress).State = EntityState.Modified; dbContext.SaveChanges(); } }