public static void InsertCustomer(NorthwindEntities northDb, string companyName = null, string contractName = null, string contractTitle = null, string address = null, string city = null, string region = null, string postalCode = null, string country = null, string phone = null, string fax = null) { using (northDb) { Customer newCustomer = new Customer(); newCustomer.CustomerID = "temporaryUnique"; newCustomer.CompanyName = companyName; newCustomer.ContactName = contractName; newCustomer.ContactTitle = contractTitle; newCustomer.Address = address; newCustomer.City = city; newCustomer.Region = region; newCustomer.PostalCode = postalCode; newCustomer.Country = country; newCustomer.Phone = phone; newCustomer.Fax = fax; northDb.Customers.Add(newCustomer); northDb.SaveChanges(); } }
public static void UpdateCustomer(NorthwindEntities northDb, string id, string companyName) { using (northDb) { Customer customerToUpdate = (Customer)northDb.Customers.Select(x => x.CustomerID == id); customerToUpdate.CompanyName = companyName; northDb.SaveChanges(); } }
public static void DeleteCustomer(NorthwindEntities northDb, string id) { using (northDb) { Customer customerToDelete = (Customer)northDb.Customers.Select(x => x.CustomerID == id); northDb.Customers.Remove(customerToDelete); northDb.SaveChanges(); } }
private static void Main() { Console.WriteLine("-- Establishing first connection to database Northwind..."); Thread.Sleep(1000); using (var firstDb = new NorthwindEntities()) { var firstCategory = firstDb.Categories.Find(4); Console.WriteLine("Initial category description: {0}", firstCategory.Description); Thread.Sleep(1000); firstCategory.Description = "Cheese and many more"; Console.WriteLine("Category description after changing: {0}", firstCategory.Description); Thread.Sleep(1000); Console.WriteLine("-- Establishing second connection to database Northwind..."); Thread.Sleep(1000); using (var secondDb = new NorthwindEntities()) { var secondCategory = secondDb.Categories.Find(4); Console.WriteLine("Initial category description: {0}", secondCategory.Description); Thread.Sleep(1000); secondCategory.Description = "Cheese and many, many more"; Console.WriteLine("Category description after changing: {0}", secondCategory.Description); Thread.Sleep(1000); firstDb.SaveChanges(); secondDb.SaveChanges(); Console.WriteLine("Category description after saving: {0}", secondCategory.Description); Thread.Sleep(1000); } Console.WriteLine("-- Closing second connection to the database..."); Thread.Sleep(1000); Console.WriteLine("Category description after saving: {0}", firstCategory.Description); Thread.Sleep(1000); } Console.WriteLine("-- Closing first connection to the database..."); using (var db = new NorthwindEntities()) { Console.WriteLine("Actual result: {0}", db.Categories.Find(4).Description); } }