/// <summary> /// Problem solved by only using 1 db connection /// </summary> public static void Main() { using (var db1 = new NorthwindEntities()) { using (var db2 = new NorthwindEntities()) { Console.WriteLine("First connection"); Console.WriteLine("Original -> " + db1.Employees.First().City); db1.Employees.First().City = "Orlandovci"; Console.WriteLine("Changed -> " + db1.Employees.First().City); Console.WriteLine("Second connection"); Console.WriteLine("Original -> " + db2.Employees.First().City); db2.Employees.First().City = "Krivina"; Console.WriteLine("Changed -> " + db2.Employees.First().City); db1.SaveChanges(); db2.SaveChanges(); } } using (var db3 = new NorthwindEntities()) { Console.WriteLine("Third connection"); Console.WriteLine(db3.Employees.First().City); } }
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) { var db = new NorthwindEntities(); using (db) { db.Customers.Add(new Customer { CustomerID = customerId, CompanyName = companyName, ContactName = contactName, ContactTitle = contactTitle, Address = address, City = city, Region = region, PostalCode = postalCode, Country = country, Phone = phone, Fax = fax }); db.SaveChanges(); } }
public static void RemoveCustomer(string customerId) { var db = new NorthwindEntities(); using (db) { var customerToRemove = db.Customers.FirstOrDefault(c => c.CustomerID == customerId); db.Customers.Remove(customerToRemove); db.SaveChanges(); } }
public static void ModifyCustomer( string customerId, string companyName, string contactName, string contactTitle, string address, string city, string region, string postalCode, string country, string phone, string fax) { var db = new NorthwindEntities(); using (db) { Customer customerToModify = db.Customers.FirstOrDefault(c => c.CustomerID == customerId); customerToModify.CompanyName = companyName ?? customerToModify.CompanyName; customerToModify.ContactName = contactName ?? customerToModify.ContactName; customerToModify.ContactTitle = contactTitle ?? customerToModify.ContactTitle; customerToModify.Address = address ?? customerToModify.Address; customerToModify.City = city ?? customerToModify.City; customerToModify.Region = region ?? customerToModify.Region; customerToModify.PostalCode = postalCode ?? customerToModify.PostalCode; customerToModify.Country = country ?? customerToModify.Country; customerToModify.Phone = phone ?? customerToModify.Phone; customerToModify.Fax = fax ?? customerToModify.Fax; db.SaveChanges(); } }