public Customer InsertCustomer(Customer customer) { using (var dbContext = new Entities.AdventureWorks2017Context()) { // Omvormen 'domein' object naar 'entity' object(en). // Een customer aanmaken komt neer op het opvullen van 3 tabellen! Entities.Customer customerEntity = new Entities.Customer(); customerEntity.Person = new Entities.Person() { PersonType = "IN", // https://www.sqldatadictionary.com/AdventureWorks2014/Person.Person.html FirstName = customer.FirstName, LastName = customer.LastName }; customerEntity.Person.BusinessEntity = new Entities.BusinessEntity() { }; customerEntity.Person.EmailAddress.Add(new Entities.EmailAddress() { EmailAddress1 = customer.Email }); // Toevoegen aan DbContext dbContext.Add(customerEntity); // Wegschrijven naar database dbContext.SaveChanges(); // Toegekende ID (door database) in domein object kopiëren. customer.Id = customerEntity.CustomerId; return(customer); } }
public Customer UpdateCustomer(Customer customer) { using (var dbContext = new Entities.AdventureWorks2017Context()) { var customerEntity = dbContext.Customer .Include(c => c.Person) .Include(c => c.Person.EmailAddress) .Where(c => c.CustomerId == customer.Id) .FirstOrDefault(); customerEntity.Person.FirstName = customer.FirstName; customerEntity.Person.LastName = customer.LastName; customerEntity.Person.EmailAddress.First().EmailAddress1 = customer.Email; dbContext.SaveChanges(); return(customer); } }
public void DeleteCustomer(int id) { using (var dbContext = new Entities.AdventureWorks2017Context()) { var customerEntity = dbContext.Customer .Include(c => c.Person) .Include(c => c.Person.EmailAddress) .Where(c => c.CustomerId == id) .FirstOrDefault(); foreach (Entities.EmailAddress ea in customerEntity.Person.EmailAddress) { dbContext.EmailAddress.Remove(ea); } dbContext.Person.Remove(customerEntity.Person); dbContext.Customer.Remove(customerEntity); dbContext.SaveChanges(); } }