public void Delete(Customer entity) { //If the entity isn't attached, attach it otherwise it won't be deleted. if (this.Context.Entry(entity).State == System.Data.EntityState.Detached) { this.Context.Customers.Attach(entity); } this.Context.Customers.Remove(entity); }
public ActionResult Create(CustomerCreateViewModel customerViewModel) { if (ModelState.IsValid) { Customer customer = new Customer(); customer.Address = customerViewModel.Address; customer.City = customerViewModel.City; customer.FirstName = customerViewModel.FirstName; customer.LastName = customerViewModel.LastName; customer.State = customerViewModel.State; customer.Zip = customerViewModel.Zip; customer.EmailAddress = customerViewModel.EmailAddress; _repository.Create(customer); _repository.Save(); return RedirectToAction("Edit", new { id = customer.CustomerId }); } return View(customerViewModel); }
public void InsertAndLoadCustomer() { //Add to context just to show its state prior to save Customer customerNew = new Customer(_validator); customerNew.FirstName = "Mary"; customerNew.LastName = "Doe"; _dbContext.Customers.Attach(customerNew); //State is 'unchanged' no errors raised by simply adding it. Debug.WriteLine(_dbContext.Entry(customerNew).State); //Insert via repository ICustomerRepository repository = new CustomerRepository(_dbContext); //creates and saves it var customer = CreateCustomer(repository); //Get its state from the context - should be unchanged (as its saved) Debug.WriteLine(_dbContext.Entry(customer).State); //Saving again does nothing to the database (see profiler) repository.Update(customer); repository.Save(); //update the first name. customer.FirstName = "John"; //Get its state from the context Debug.WriteLine(_dbContext.Entry(customer).State); //saving again notice only the first name is updated. repository.Update(customer); repository.Save(); Customer insertCustomer = new Customer(_validator); insertCustomer.FirstName = "Jane"; insertCustomer.LastName = "Doe"; insertCustomer.Address = "555 main st"; insertCustomer.City = "Orlando"; insertCustomer.Zip = "33400"; insertCustomer.State = "FL"; Debug.WriteLine(insertCustomer.CustomerId); _dbContext.Customers.Add(insertCustomer); Debug.WriteLine(_dbContext.Entry(insertCustomer).State); _dbContext.SaveChanges(); //Note the primary key is automatically updated. Debug.WriteLine(insertCustomer.CustomerId); Debug.WriteLine(_dbContext.Entry(insertCustomer).State); }
private Customer CreateCustomer(ICustomerRepository repository) { Customer customer = new Customer(_validator) { FirstName = "Test", LastName = "User", City = "Bethlehem", State = "PA", Zip = "18018", Address = "555 main st" }; repository.Create(customer); repository.Save(); return customer; }
public void TestNoKey() { ICustomerRepository repository = new CustomerRepository(_dbContext); var bip = new Customer(_validator); bip.CustomerId = 10; bip.CustomerId = 20; var customer = CreateCustomer(repository); _dbContext.Customers.Attach(bip); bip.CustomerId = 30; Debug.WriteLine(_dbContext.Entry(bip).State); var newContext = new EntityContext(); var repository2 = new CustomerRepository(newContext); Debug.WriteLine(newContext.Entry(customer).State); customer.Address = "555"; Debug.WriteLine(newContext.Entry(customer).State); //newContext.Entry(customer).State = System.Data.EntityState.Modified; Debug.WriteLine(newContext.Entry(customer).State); newContext.Customers.Attach(customer); customer.CustomerId = 0; newContext.SaveChanges(); repository2.Update(customer); repository2.Save(); customer.Address = "556"; Debug.WriteLine(newContext.Entry(customer).State); }
public void TestCustomerLoading() { var customer = _dbContext.Customers.Where(o => o.CustomerId == 1); ICustomerRepository repository = new CustomerRepository(_dbContext); Debug.WriteLine(customer.Single().FirstName); var customers = repository.GetAll(); //Debug.Assert(customers.Count == 0); Customer customer2 = new Customer(_validator) { FirstName = "Test", City = "bethlehem" }; repository.Create(customer2); repository.Save(); }
public void TestAddCustomerMissingRequiredFields() { using (EntityContext context = new EntityContext()) { Customer customer = new Customer(_validator); ICustomerRepository repository = new CustomerRepository(context); repository.Create(customer); repository.Save(); } }
public void ShowSmartFieldUpating() { ICustomerRepository repository = new CustomerRepository(_dbContext); var customers = repository.GetAll(); Debug.Assert(customers.Count() == 0); Customer customer = new Customer(_validator) { FirstName = "Test", City = "bethlehem" }; repository.Create(customer); repository.Save(); customer.Address = "1 main st" + DateTime.Now.ToString(); customer.City = customer.City; repository.Update(customer); repository.Save(); }
/// <summary> /// Gives a customer to the context, does NOT save it to the db. Save() must be called to commit changes. /// </summary> /// <param name="customer"></param> public void Create(Customer customer) { this.Context.Customers.Add(customer); }
/// <summary> /// Saves a customer. This also ensures it's part of the context (a requirement for saving) /// </summary> /// <param name="customer"></param> public void Update(Customer customer) { //In case the new guy on the team sends in a detached entity. if (this.Context.Entry(customer).State == System.Data.EntityState.Detached) { this.Context.Entry(customer).State = System.Data.EntityState.Modified; } else { //In MVC we're overwriting the object's properties with a timestamp. //The timestamp though is treated by EF as a computed field and only the original valyeds this.Context.Entry(customer).Property(u => u.Timestamp).OriginalValue = customer.Timestamp; } Context.SaveChanges(); }