示例#1
0
        public static void Insert(string customerId, string companyName, string address = null,
            string city = null, string contactName = null, string contactTitle = null, string country = null,
            string fax = null, string phone = null, string postalCode = null, string region = null)
        {
            using (NorthwindEntities dbContext = new NorthwindEntities())
            {
                Customer newCustomer = new Customer()
                {
                    Address = address,
                    City = city,
                    CompanyName = companyName,
                    ContactName = contactName,
                    ContactTitle = contactTitle,
                    Country = country,
                    CustomerID = customerId,
                    Fax = fax,
                    Phone = phone,
                    PostalCode = postalCode,
                    Region = region
                };

                dbContext.Customers.Add(newCustomer);
                dbContext.SaveChanges();
            }
        }
示例#2
0
 public static int AddCustomer(Customer newCustomer)
 {
     using (var dataBase = new NorthwindEntities())
     {
         dataBase.Customers.Add(newCustomer);
         return dataBase.SaveChanges();
     }
 }
示例#3
0
 /// <summary>
 /// Adds a customer to customers table.
 /// </summary>
 /// <param name="customer">The customer to be added.</param>
 public static void AddCustomer(Customer customer)
 {
     using (var northwindDbContext = new NorthwindEntities())
     {
         northwindDbContext.Customers.Add(customer);
         northwindDbContext.SaveChanges();
         Console.WriteLine("Customer successfully added!");
     }
 }
示例#4
0
 public static int DeleteCustomer(string id)
 {
     using (var dataBase = new NorthwindEntities())
     {
         Customer customer = dataBase.Customers.First(x => x.CustomerID == id);
         dataBase.Customers.Remove(customer);
         return dataBase.SaveChanges();
     }
 }
示例#5
0
        //2. Create a DAO class with static methods which provide functionality 
        //for inserting, modifying and deleting customers. Write a testing class.

        public static void Delete(string customerId)
        {
            using (NorthwindEntities dbContext = new NorthwindEntities())
            {
                Customer customer = dbContext.Customers.Find(customerId);
                dbContext.Customers.Remove(customer);
                dbContext.SaveChanges();
            }
        }
示例#6
0
 public static int UpdateContactNameTitle(Customer customer, string contactName, string contactTitle)
 {
     using (var northwindEntities = new NorthwindEntities())
     {
         northwindEntities.Customers.Attach(customer);
         customer.CompanyName = contactName;
         customer.ContactTitle = contactTitle;
         return northwindEntities.SaveChanges();
     }
 }
示例#7
0
        /* 02. Create a DAO class with static methods which provide functionality for inserting, modifying and deleting customers. Write a testing class.*/
        /// <summary>
        /// Deletes a customer identified by CustomerID.
        /// </summary>
        /// <param name="customer">The customer.</param>
        public static void DeleteCustomer(Customer customer)
        {
            using (var northwindDbContext = new NorthwindEntities())
            {
                var customerForDelete = northwindDbContext.Customers.First(c => c.CustomerID == customer.CustomerID);
                northwindDbContext.Customers.Remove(customerForDelete);

                northwindDbContext.SaveChanges();
                Console.WriteLine("Customer successfully deleted!");
            }
        }
示例#8
0
 public static void Modify(string customerId, string companyName /*= null, string address = null,
     string city = null, string contactName = null, string contactTitle = null, string country = null,
     string fax = null, string phone = null, string postalCode = null, string region = null*/)
 {
     using (NorthwindEntities dbContext = new NorthwindEntities())
     {
         Customer customer = dbContext.Customers.Find(customerId);
         customer.CompanyName = companyName;
         dbContext.SaveChanges();
     }
 }
示例#9
0
        /* 07. Try to open two different data contexts and perform concurrent changes on the same records. What will happen at SaveChanges()? How to deal with it?*/
        // EF executes everything in transactions by default. We don`t need to worry about it.
        /// <summary>
        /// Mains this instance.
        /// </summary>
        public static void Main()
        {
            using (var northwindDbContext = new NorthwindEntities())
            {
                using (var secondNorthwindDbContext = new NorthwindEntities())
                {
                    var order = northwindDbContext.Orders.Find(10248);
                    var sameOrder = secondNorthwindDbContext.Orders.Find(10248);

                    // Mark for delete
                    northwindDbContext.Orders.Remove(order);

                    // Update some stuff
                    sameOrder.ShipCountry = "Bulgaria";

                    // Make update
                    try
                    {
                        secondNorthwindDbContext.SaveChanges();
                        Console.WriteLine("Updating complete!");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Updating failed!");
                    }

                    // Delete -> Produces an exception because of data integrity.
                    try
                    {
                        northwindDbContext.SaveChanges();
                        Console.WriteLine("Deleting complete!");
                    }
                    catch (Exception)
                    {

                        Console.WriteLine("Delete cannot be executed!");
                    }

                }
            }

            Console.WriteLine("Done!");
        }
示例#10
0
        public static int AddOrder(Order newOrder)
        {
            using (var dataBase = new NorthwindEntities())
            {
                var customer = dataBase.Customers.First(c => c.CustomerID == newOrder.CustomerID);
                if (customer != null)
                {
                    newOrder.ShipAddress = customer.Address;
                    newOrder.ShipCity = customer.City;
                    newOrder.ShipPostalCode = customer.PostalCode;
                    newOrder.ShipCountry = customer.Country;
                }

                dataBase.Orders.Add(newOrder);
                dataBase.Orders.Add(newOrder);
                dataBase.Orders.Add(newOrder);
                return dataBase.SaveChanges();
            }
        }
示例#11
0
        /// <summary>
        /// Adds an order multiple times to the db. Transaction demo.
        /// </summary>
        /// <param name="order">The order.</param>
        private static void AddOrderMultipleTimes(Order order)
        {
            using (var northwindDbContext = new NorthwindEntities())
            {
                northwindDbContext.Orders.Add(order);
                northwindDbContext.Orders.Add(order);
                northwindDbContext.Orders.Add(order);

                //// Uncomment to see transaction power!
                // order.CustomerID = "PESHO_ZLIA";
                northwindDbContext.Orders.Add(order);
                northwindDbContext.Orders.Add(order);

                try
                {
                    northwindDbContext.SaveChanges();
                }
                catch (Exception)
                {
                    Console.WriteLine("Error! Orders not confirmed!");
                }
            }
        }
示例#12
0
        private static void PlaceNewOrder(
            int orderId, string customerId = null,
            int? employeeId = null, DateTime? orderDate = null,
            DateTime? requireDate = null, DateTime? shippedDate = null,
            int? shipVia = null, decimal? freight = null,
            string shipName = null, string shipAddress = null,
            string shipCity = null, string shipRegion = null,
            string shipPostalCode = null, string shipCountry = null)
        {
            //9. Create a method that places a new order in the Northwind database. The order 
            //should contain several order items. Use transaction to ensure the data consistency.

            using (NorthwindEntities dbContext = new NorthwindEntities())
            {
                Order newOrder = new Order()
                {
                    CustomerID = customerId,
                    EmployeeID = employeeId,
                    Freight = freight,
                    OrderDate = orderDate,
                    OrderID = orderId,
                    RequiredDate = requireDate,
                    ShipAddress = shipAddress,
                    ShipCity = shipCity,
                    ShipCountry = shipCountry,
                    ShipName = shipName,
                    ShippedDate = shippedDate,
                    ShipPostalCode = shipPostalCode,
                    ShipRegion = shipRegion,
                    ShipVia = shipVia,
                };

                dbContext.Orders.Add(newOrder);

                dbContext.SaveChanges();
            }
        }
示例#13
0
        private static void MakeConcurencyChanges()
        {
            //7. Try to open two different data contexts and perform concurrent changes 
            //on the same records. What will happen at SaveChanges()? How to deal with it?

            NorthwindEntities dbContext1 = new NorthwindEntities();
            Employee employee1 = dbContext1.Employees.Find(10);
            //employee1.City = "Burgas";
            dbContext1.Employees.Remove(employee1);

            //dbContext1.SaveChanges();

            NorthwindEntities dbContext2 = new NorthwindEntities();
            Employee employee2 = dbContext2.Employees.Find(10);
            employee2.City = "Varna";

            dbContext1.SaveChanges();
            dbContext2.SaveChanges();

            dbContext1.Dispose();
            dbContext2.Dispose();
        }
示例#14
0
        /// <summary>
        /// Updates a customer identified by CustomerID.
        /// </summary>
        /// <param name="customer">The customer.</param>
        public static void UpdateCustomer(Customer customer)
        {
            using (var northwindDbContext = new NorthwindEntities())
            {
                var customerForUpdate = northwindDbContext.Customers.First(c => c.CustomerID == customer.CustomerID);

                customerForUpdate.CompanyName = customer.CompanyName ?? customerForUpdate.CompanyName;
                customerForUpdate.ContactName = customer.ContactName ?? customerForUpdate.ContactName;
                customerForUpdate.ContactTitle = customer.ContactTitle ?? customerForUpdate.ContactTitle;
                customerForUpdate.Address = customer.Address ?? customerForUpdate.Address;
                customerForUpdate.City = customer.City ?? customerForUpdate.City;
                customerForUpdate.Region = customer.Region ?? customerForUpdate.Region;
                customerForUpdate.PostalCode = customer.PostalCode ?? customerForUpdate.PostalCode;
                customerForUpdate.Country = customer.Country ?? customerForUpdate.Country;
                customerForUpdate.Phone = customer.Phone ?? customerForUpdate.Phone;
                customerForUpdate.Fax = customer.Fax ?? customerForUpdate.Fax;

                northwindDbContext.SaveChanges();
                Console.WriteLine("The customer is Updated!");
            }
        }