static void Main(string[] args) { using (var firstContext = new NorthwindEntities()) { using (var secondContext = new NorthwindEntities()) { firstContext.Customers.Add(new Customer { CustomerID = 5000, CompanyName = "CONFLICTTEST" }); secondContext.Customers.Add(new Customer {CustomerID = 6000, CompanyName = "CONFLICTTEST2"}); secondContext.SaveChanges(); firstContext.SaveChanges(); secondContext.SaveChanges(); } } }
public void AddCustomer(Customer customer) { using (var context = new NorthwindEntities()) { context.Customers.Add(customer); context.SaveChanges(); } }
public void DeleteCustomerById(int id) { using (var context = new NorthwindEntities()) { var customer = context.Customers.FirstOrDefault(c => c.CustomerID == id); context.Customers.Remove(customer); context.SaveChanges(); } }
public Customer RemoveCustomer(Customer customer) { using (var context = new NorthwindEntities()) { context.Customers.Remove(customer); context.SaveChanges(); } return customer; }
public Customer ModifyCostomer(int id, string companyName) { using (var context = new NorthwindEntities()) { var customer = context.Customers.SingleOrDefault(x => x.CustomerID == id); if (customer!=null) { customer.CompanyName = companyName; } context.SaveChanges(); return customer; } }
public static void AddOrder(string shipName, short amount, int productID, decimal unitPrice, float discount) { using (NorthwindEntities dbContent = new NorthwindEntities()) { using (TransactionScope scope = new TransactionScope()) { try { Order newOrder = new Order { ShipName = shipName }; dbContent.Orders.Add(newOrder); dbContent.SaveChanges(); var result = dbContent.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; } } } }