static void Main() { using (NorthwindEntities dbFirst = new NorthwindEntities()) { using (NorthwindEntities dbSecond = new NorthwindEntities()) { dbFirst.Customers.Add(new Customer() { CustomerID = "xswde", CompanyName = "CONFLICTTEST" }); dbSecond.Customers.Add(new Customer() { CustomerID = "xldde", CompanyName = "CONFLICTTEST2" }); dbSecond.SaveChanges(); dbFirst.SaveChanges(); dbSecond.SaveChanges(); } } }
public static void UpdateCustomer(string customerID, string newCompanyName) { NorthwindEntities db = new NorthwindEntities(); using (db) { Customer customerToUpdate = FindCustomerById(customerID); db.Entry(customerToUpdate).State = EntityState.Modified; customerToUpdate.CompanyName = newCompanyName; db.SaveChanges(); Console.WriteLine("Customer with ID:{0} UPDATED Successfully", customerID); } }
public static void DeleteCustomer(string customerID) { NorthwindEntities db = new NorthwindEntities(); using (db) { Customer customerToRemove = FindCustomerById(customerID); db.Entry(customerToRemove).State = EntityState.Deleted; db.Customers.Remove(customerToRemove); db.SaveChanges(); Console.WriteLine("Customer with ID:{0} DELETED Successfully", customerID); } }
public static void AddCustomer(string customerID, string companyName) { NorthwindEntities db = new NorthwindEntities(); using (db) { Customer newCustomer = new Customer() { CustomerID = customerID, CompanyName = companyName }; db.Customers.Add(newCustomer); db.SaveChanges(); Console.WriteLine("Customer with ID:{0} and CompanyName: {1} Recorded Successfully", customerID, companyName); } }
private static void AddOrder(string shipName, short amount, int productID, decimal unitPrice, float discount) { NorthwindEntities db = new NorthwindEntities(); using (db) { TransactionScope scope = new TransactionScope(); using (scope) { try { Order newOrder = new Order { ShipName = shipName }; db.Orders.Add(newOrder); db.SaveChanges(); var result = db.Orders.Where(x => x.ShipName == shipName); using (NorthwindEntities dbContext = new NorthwindEntities()) { foreach (var item in result) { Order_Detail newDetail = new Order_Detail { OrderID = item.OrderID, ProductID = productID, UnitPrice = unitPrice, Discount = discount, Quantity = amount, }; dbContext.Order_Details.Add(newDetail); dbContext.SaveChanges(); } } scope.Complete(); } catch (Exception e) { throw e; } } } }