示例#1
0
        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();
                }
            }
        }
示例#2
0
 public void AddCustomer(Customer customer)
 {
     using (var context = new NorthwindEntities())
     {
         context.Customers.Add(customer);
         context.SaveChanges();
     }
 }
示例#3
0
 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();
     }
 }
示例#4
0
        public Customer RemoveCustomer(Customer customer)
        {
            using (var context = new NorthwindEntities())
            {
                context.Customers.Remove(customer);
                context.SaveChanges();
            }

            return customer;
        }
示例#5
0
        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;
            }
        }
示例#6
0
        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;
                    }
                }
            }
        }