public static void AddCustomer(string customerID, string companyName, string contactName = null, string contactTitle = null, string address = null, string city = null, string region = null, string postalCode = null, string country = null, string phone = null, string fax = null) { using (NorthwindEntities DBcontext = new NorthwindEntities()) { Customer customer = new Customer() { CustomerID = customerID, CompanyName = companyName, ContactName = contactName, ContactTitle = contactTitle, Address = address, City = city, Region = region, PostalCode = postalCode, Country = country, Phone = phone, Fax = fax }; DBcontext.Customers.Add(customer); DBcontext.SaveChanges(); Console.WriteLine("Row is inserted"); } }
public static void DeleteCustomer(string customerID) { using (NorthwindEntities DBcontext = new NorthwindEntities()) { try { Customer customer = DBcontext.Customers.First(c => c.CustomerID == customerID); DBcontext.Customers.Remove(customer); DBcontext.SaveChanges(); Console.WriteLine("Row is deleted"); } catch (InvalidOperationException e) { throw new InvalidOperationException("There is no customer with this ID", e); } } }
public static void UpdateCustomer(string customerID, string companyName, string contactName = null, string contactTitle = null, string address = null, string city = null, string region = null, string postalCode = null, string country = null, string phone = null, string fax = null) { using (NorthwindEntities DBcontext = new NorthwindEntities()) { Customer customer = DBcontext.Customers.First(x => x.CustomerID == customerID); customer.CompanyName = companyName ?? customer.CompanyName; customer.ContactName = contactName ?? customer.ContactName; customer.ContactTitle = contactTitle ?? customer.ContactTitle; customer.Address = address ?? customer.Address; customer.City = city ?? customer.City; customer.Region = region ?? customer.Region; customer.PostalCode = postalCode ?? customer.PostalCode; customer.Country = country ?? customer.Country; customer.Phone = phone ?? customer.Phone; customer.Fax = fax ?? customer.Fax; DBcontext.SaveChanges(); Console.WriteLine("Row is updated"); } }