示例#1
0
        public static void DeleteCustomer(string customerId)
        {
            NorthwindEntities northwindEntities = new NorthwindEntities();
            Customer customerToDelete = northwindEntities.Customers.FirstOrDefault(c => c.CustomerID == customerId);
            if (customerToDelete == null)
            {
                throw new System.Data.EntityException("There is no such customer.");
            }

            northwindEntities.Customers.Remove(customerToDelete);
            northwindEntities.SaveChanges();
        }
示例#2
0
        public static void Main()
        {
            NorthwindEntities firstContext = new NorthwindEntities();
            NorthwindEntities secondContext = new NorthwindEntities();

            ModifyCustomer(firstContext, "ALFKI", city: "Moscow");
            ModifyCustomer(secondContext, "ALFKI", city: "London");

            // Since database is not locked, the last modification will be the one to remain. According to MSDN:
            // http://msdn.microsoft.com/en-us/library/bb739065.aspx SaveChanges(boolean) is obsolete and should
            // not be used.
            firstContext.SaveChanges();
            secondContext.SaveChanges();

            firstContext.Dispose();
            secondContext.Dispose();
        }
示例#3
0
        public static void InsertCustomer(string customerId, string companyName, string contactName = null, string address = null,
            string city = null, string region = null, string postalCode = null, string country = null, string phone = null, 
            string fax = null)
        {
            NorthwindEntities northwindEntities = new NorthwindEntities();

            int numberOfCustomersBefore =
                (from customer in northwindEntities.Customers
                select customer).Count();

            Customer customerToInsert = new Customer
            {
                CustomerID = customerId,
                CompanyName = companyName,
                Address = address,
                City = city,
                PostalCode = postalCode,
                Region = region,
                Country = country,
                Phone = phone,
                Fax = fax
            };

            northwindEntities.Customers.Add(customerToInsert);
            northwindEntities.SaveChanges();

            int numberOfCustomersAfter =
                (from customer in northwindEntities.Customers
                 select customer).Count();

            if (numberOfCustomersAfter - numberOfCustomersBefore == 1)
            {
                Console.WriteLine("Customer added successfully.");
            }
            else
            {
                throw new System.Data.EntityException("Incorrect insertion result.");
            }
        }
示例#4
0
        public static void ModifyCustomer(string customerId, string companyName = null, string contactName = null, string address = null,
            string city = null, string region = null, string postalCode = null, string country = null, string phone = null,
            string fax = null)
        {
            NorthwindEntities northwindEntities = new NorthwindEntities();
            Customer customerToModify = northwindEntities.Customers.FirstOrDefault(c => c.CustomerID == customerId);
            if (customerToModify == null)
            {
                throw new System.Data.EntityException("No such customer exists.");
            }

            customerToModify.CompanyName = string.IsNullOrEmpty(companyName) ? customerToModify.CompanyName : companyName;
            customerToModify.ContactName = string.IsNullOrEmpty(contactName) ? customerToModify.ContactName : contactName;
            customerToModify.Address = string.IsNullOrEmpty(address) ? customerToModify.Address : address;
            customerToModify.City = string.IsNullOrEmpty(city) ? customerToModify.City : city;
            customerToModify.Region = string.IsNullOrEmpty(region) ? customerToModify.Region : region;
            customerToModify.PostalCode = string.IsNullOrEmpty(postalCode) ? customerToModify.PostalCode : postalCode;
            customerToModify.Country = string.IsNullOrEmpty(country) ? customerToModify.Country : country;
            customerToModify.Phone = string.IsNullOrEmpty(phone) ? customerToModify.Phone : phone;
            customerToModify.Fax = string.IsNullOrEmpty(fax) ? customerToModify.Fax : fax;

            northwindEntities.SaveChanges();
        }
示例#5
0
        public static void InsertOrder(int shipperId, string customerId, int employeeId, string destionation)
        {
            using (NorthwindEntities northwindEntities = new NorthwindEntities())
            {
                try
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        Order orderToInsert = new Order
                        {
                            CustomerID = customerId,
                            EmployeeID = employeeId,
                            OrderDate = DateTime.Now,
                            RequiredDate = DateTime.Now.Add(new TimeSpan(100, 0, 0, 0)),
                            ShipVia = shipperId,
                            Freight = 3000000,
                            ShipCountry = destionation,
                            Customer = northwindEntities.Customers.FirstOrDefault(c => c.CustomerID == customerId),
                            Employee = northwindEntities.Employees.FirstOrDefault(e => e.EmployeeID == employeeId),
                            Shipper = northwindEntities.Shippers.FirstOrDefault(s => s.ShipperID == shipperId),
                            Order_Details = new HashSet<Order_Detail>()
                        };

                        orderToInsert.Order_Details.Add(new Order_Detail
                        {
                            ProductID = 2,
                            Quantity = 34,
                            Discount = 1,
                            Order = orderToInsert
                        });

                        orderToInsert.Order_Details.Add(new Order_Detail
                        {
                            ProductID = 3,
                            Quantity = 70,
                            Discount = 1,
                            Order = orderToInsert
                        });

                        orderToInsert.Order_Details.Add(new Order_Detail
                        {
                            ProductID = 4,
                            Quantity = 110,
                            Discount = 1,
                            Order = orderToInsert
                        });
                        
                        northwindEntities.Orders.Add(orderToInsert);                       

                        northwindEntities.SaveChanges();

                        scope.Complete();
                         //This ensures that the transaction has been completed.
                        int orderId = orderToInsert.OrderID;
                        Console.WriteLine(orderId);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }