public static void Insert(string customerId, string companyName, string address = null, string city = null, string contactName = null, string contactTitle = null, string country = null, string fax = null, string phone = null, string postalCode = null, string region = null) { using (NorthwindEntities dbContext = new NorthwindEntities()) { Customer newCustomer = new Customer() { Address = address, City = city, CompanyName = companyName, ContactName = contactName, ContactTitle = contactTitle, Country = country, CustomerID = customerId, Fax = fax, Phone = phone, PostalCode = postalCode, Region = region }; dbContext.Customers.Add(newCustomer); dbContext.SaveChanges(); } }
/// <summary> /// Adds a customer to customers table. /// </summary> /// <param name="customer">The customer to be added.</param> public static void AddCustomer(Customer customer) { using (var northwindDbContext = new NorthwindEntities()) { northwindDbContext.Customers.Add(customer); northwindDbContext.SaveChanges(); Console.WriteLine("Customer successfully added!"); } }
/* 02. Create a DAO class with static methods which provide functionality for inserting, modifying and deleting customers. Write a testing class.*/ /// <summary> /// Deletes a customer identified by CustomerID. /// </summary> /// <param name="customer">The customer.</param> public static void DeleteCustomer(Customer customer) { using (var northwindDbContext = new NorthwindEntities()) { var customerForDelete = northwindDbContext.Customers.First(c => c.CustomerID == customer.CustomerID); northwindDbContext.Customers.Remove(customerForDelete); northwindDbContext.SaveChanges(); Console.WriteLine("Customer successfully deleted!"); } }
/* 02. Create a DAO class with static methods which provide functionality for inserting, modifying and deleting customers. Write a testing class.*/ /// <summary> /// Mains this instance. /// </summary> public static void Main() { var pesho = new Customer { CustomerID = "ZzPS3", CompanyName = "TElerikAcademy", ContactName = "Pesho", ContactTitle = "Ninja", }; DAO.AddCustomer(pesho); pesho.ContactTitle = "AcademyNinja"; DAO.UpdateCustomer(pesho); DAO.DeleteCustomer(pesho); }
/// <summary> /// Updates a customer identified by CustomerID. /// </summary> /// <param name="customer">The customer.</param> public static void UpdateCustomer(Customer customer) { using (var northwindDbContext = new NorthwindEntities()) { var customerForUpdate = northwindDbContext.Customers.First(c => c.CustomerID == customer.CustomerID); customerForUpdate.CompanyName = customer.CompanyName ?? customerForUpdate.CompanyName; customerForUpdate.ContactName = customer.ContactName ?? customerForUpdate.ContactName; customerForUpdate.ContactTitle = customer.ContactTitle ?? customerForUpdate.ContactTitle; customerForUpdate.Address = customer.Address ?? customerForUpdate.Address; customerForUpdate.City = customer.City ?? customerForUpdate.City; customerForUpdate.Region = customer.Region ?? customerForUpdate.Region; customerForUpdate.PostalCode = customer.PostalCode ?? customerForUpdate.PostalCode; customerForUpdate.Country = customer.Country ?? customerForUpdate.Country; customerForUpdate.Phone = customer.Phone ?? customerForUpdate.Phone; customerForUpdate.Fax = customer.Fax ?? customerForUpdate.Fax; northwindDbContext.SaveChanges(); Console.WriteLine("The customer is Updated!"); } }