示例#1
0
 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();
         }
     }
 }
示例#2
0
 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);
     }
 }
示例#3
0
 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);
     }
 }
示例#4
0
 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);
     }
 }
示例#5
0
        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;
                    }
                }
            }
        }