public void DeleteCustomer_TestIsCustomerDeleted() { using (var db = new NORTHWNDEntities()) { string companyName = "Telerik"; Customer testCustomer = new Customer() { CompanyName = companyName, CustomerID = "ABC12" }; CustomerOperations.AddCustomer(testCustomer); // Using ToList() to force execution var numberOfCustomersAfterAdding = (from c in db.Customers where c.CompanyName == companyName select c).ToList(); // Delete customer added for test CustomerOperations.DeleteCustomer(testCustomer); var numberOfCustomersAfterDeleting = (from c in db.Customers where c.CompanyName == companyName select c).ToList(); Assert.AreEqual(numberOfCustomersAfterAdding.Count(), numberOfCustomersAfterDeleting.Count() + 1, "Check is customer deleted"); } }
public void AddCustomer_CheckIsFieldsCorrectAdded() { using (var db = new NORTHWNDEntities()) { string companyName = "Telerik"; string customerID = "ABC12"; Customer testCustomer = new Customer() { CompanyName = companyName, CustomerID = customerID }; CustomerOperations.AddCustomer(testCustomer); var getAddedCustomer = (from c in db.Customers where c.CompanyName == companyName select c).ToList(); Assert.AreEqual(getAddedCustomer.FirstOrDefault().CompanyName, companyName, "Company name is incorect."); Assert.AreEqual(getAddedCustomer.FirstOrDefault().CustomerID, customerID, "CustomerID is incorect"); // Delete customer added for test CustomerOperations.DeleteCustomer(testCustomer); } }
public static void AddCustomer(Customer customer) { using (var db = new NORTHWNDEntities()) { db.Customers.Add(customer); db.SaveChanges(); } Console.WriteLine("Customer added."); }
public static void Main() { using (var db = new NORTHWNDEntities()) { string companyName = "Telerik"; var telerikCustomers = from c in db.Customers where c.CompanyName == companyName select c; foreach (var customer in telerikCustomers) { db.Customers.Remove(customer); } db.SaveChanges(); // Create and add customer in NORTHWND database Customer testCustomer = new Customer() { CompanyName = companyName, CustomerID = "ABC42" }; Console.WriteLine("First customer is added"); db.Customers.Add(testCustomer); Console.WriteLine("Changes saved"); db.SaveChanges(); // Create two other customers and try to add them in NORTHWND database // If we try to add 'incorrectTestCustomer' SaveChanges will rollback operation (no customers added) // If we comment adding 'incorrectTestCustomer' this will work correct (will commit changes) Customer secondTestCustomer = new Customer() { CompanyName = companyName, CustomerID = "ABC98" }; Customer incorrectTestCustomer = new Customer() { CompanyName = companyName, CustomerID = "ABC98" }; Console.WriteLine("Second customer is added"); db.Customers.Add(secondTestCustomer); Console.WriteLine("Adding incorrect Customer"); // Uncomment line below. This will trow exception and call rollback. //db.Customers.Add(incorrectTestCustomer); //Console.WriteLine("Trying to save changes. This will throw exception"); db.SaveChanges(); } }
public static void ModifyCustomerCompanyName(Customer customer, string newName) { using (var db = new NORTHWNDEntities()) { var customerFromDB = db.Customers.FirstOrDefault(x => x.CustomerID == customer.CustomerID); customerFromDB.CompanyName = newName; db.SaveChanges(); } Console.WriteLine("Customer updated."); }
public static void DeleteCustomer(Customer customer) { using (var db = new NORTHWNDEntities()) { var customerFromDB = db.Customers.FirstOrDefault(x => x.CustomerID == customer.CustomerID); db.Customers.Remove(customerFromDB); db.SaveChanges(); } Console.WriteLine("Customer deleted."); }
public static void Main() { using (var db = new NORTHWNDEntities()) { Customer testCustomer = new Customer() { CompanyName = "Telerik", CustomerID = "ABC12" }; CustomerOperations.AddCustomer(testCustomer); //CustomerOperations.DeleteCustomer(testCustomer); //CustomerOperations.ModifyCustomerCompanyName(testCustomer, "Telerik Academy"); } }
public void ModifyCustomerCompanyName_TestIsCompanyNameModified() { using (var db = new NORTHWNDEntities()) { string companyName = "Telerik"; string companyNewName = "Telerik Academy"; Customer testCustomer = new Customer() { CompanyName = companyName, CustomerID = "ABC12" }; CustomerOperations.AddCustomer(testCustomer); CustomerOperations.ModifyCustomerCompanyName(testCustomer, companyNewName); var getModifiedCustomer = (from c in db.Customers where c.CompanyName == companyNewName select c).ToList(); // Delete customer added for test CustomerOperations.DeleteCustomer(testCustomer); Assert.AreEqual(companyNewName, getModifiedCustomer.FirstOrDefault().CompanyName, "Company name is not modified"); } }
/// <summary> /// Create a new Customer object. /// </summary> /// <param name="customerID">Initial value of the CustomerID property.</param> /// <param name="companyName">Initial value of the CompanyName property.</param> /// <param name="address">Initial value of the Address property.</param> public static Customer CreateCustomer(global::System.String customerID, global::System.String companyName, CommonAddress address) { Customer customer = new Customer(); customer.CustomerID = customerID; customer.CompanyName = companyName; customer.Address = StructuralObject.VerifyComplexObjectIsNotNull(address, "Address"); return customer; }
/// <summary> /// Deprecated Method for adding a new object to the Customers EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCustomers(Customer customer) { base.AddObject("Customers", customer); }