Example #1
0
 public static void DeleteCustomer(string customerId)
 {
     NorthwindEntities context = new NorthwindEntities();
     Customer customer = context.Customers.Where(x => x.CustomerID == customerId).FirstOrDefault();
     context.Customers.Remove(customer);
     context.SaveChanges();
 }
Example #2
0
        public static string InsertCustomer(string customerId, string companyName)
        {
            NorthwindEntities context = new NorthwindEntities();
            Customer customer = new Customer
            {
                CustomerID = customerId,
                CompanyName = companyName
            };

            context.Customers.Add(customer);
            context.SaveChanges();
            return customerId;
        }
Example #3
0
        static void Main(string[] args)
        {
            NorthwindEntities context = new NorthwindEntities();
            double income = context.FindTotalIncomeBySupplier("Leka Trading").First().Value;
            Console.WriteLine(income);

            income = context.FindTotalIncomeByPeriod(new DateTime(1997, 1, 1), new DateTime(1997, 6, 6)).First().Value;
            Console.WriteLine(income);

            income = context.FindTotalIncomeBySupplierAndPeriod("Leka Trading",
                new DateTime(1997, 1, 1), new DateTime(1997, 6, 6)).First().Value;
            Console.WriteLine(income);
        }
Example #4
0
        public static void UpdateRegion()
        {
            NorthwindEntities context1 = new NorthwindEntities();
            Region region1 = context1.Regions.First();

            region1.RegionDescription = "Africa";

            NorthwindEntities context2 = new NorthwindEntities();
            Region region2 = context2.Regions.First();

            region2.RegionDescription = "Europe";

            context2.SaveChanges();
            context1.SaveChanges();

            // use transactions
        }
Example #5
0
        static void FindAllCustomers(DateTime year, string country)
        {
            NorthwindEntities context = new NorthwindEntities();
            string nativeSQLQuery =
                "SELECT c.CustomerID, o.OrderDate, o.ShipCountry " +
                "FROM Customers c " +
                "JOIN Orders o " +
                "ON c.CustomerID = o.CustomerID " +
                "WHERE o.ShipCountry = {0} AND DATEPART(year, o.OrderDate) = DATEPART(year, {1})";

            var customersWithOrders = context.Database.SqlQuery<CustomerAndOrder>(nativeSQLQuery, country, year);

            foreach (var customer in customersWithOrders)
            {
                Console.WriteLine("{0} ordered on {1} from {2}", customer.CustomerID, customer.OrderDate, customer.ShipCountry);
            }
        }
Example #6
0
        static void InsertDuplicateOrders()
        {
            NorthwindEntities context = new NorthwindEntities();
            Order order = new Order
            {
                OrderID = 1,
                CustomerID = "QWE"
            };

            Order order2 = new Order
            {
                OrderID = 1,
                CustomerID = "QWE"
            };

            context.Orders.Add(order);
            context.Orders.Add(order2);
            context.SaveChanges();
        }
Example #7
0
        static void InsertDifferentOrders()
        {
            NorthwindEntities context = new NorthwindEntities();
            Order order1 = new Order
            {
                OrderID = 2,
                CustomerID = "QWE",
            };

            Order order2 = new Order
            {
                OrderID = 3,
                CustomerID = "QWE"
            };

            context.Orders.Add(order1);
            context.Orders.Add(order2);
            context.SaveChanges();
        }
Example #8
0
        static void FindAllCustomers(DateTime year, string country)
        {
            NorthwindEntities context = new NorthwindEntities();
            var customersWithOrders =
                from customer in context.Customers
                join order in context.Orders
                on customer.CustomerID equals order.CustomerID
                where order.ShipCountry == country && order.OrderDate.Value.Year == year.Year
                select new
                {
                    CustomerID = customer.CustomerID,
                    OrderDate = order.OrderDate,
                    ShipCountry = order.ShipCountry
                };

            foreach (var customer in customersWithOrders)
            {
                Console.WriteLine("{0} ordered on {1} from {2}", customer.CustomerID, customer.OrderDate, customer.ShipCountry);
            }
        }
Example #9
0
        static void FindAllOrders(string region, DateTime? startDate, DateTime? endDate)
        {
            var context = new NorthwindEntities();
            (context as IObjectContextAdapter)
                .ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior = true;
            var orders =
                from order in context.Orders
                where order.ShipRegion == region &&
                    order.OrderDate.Value >= startDate && order.OrderDate.Value <= endDate
                select order;

            foreach (var order in orders)
            {
                Console.WriteLine("{0} has ordered {1} on {2} and received the order on {3} in {4}",
                    order.CustomerID, order.OrderID,
                    order.OrderDate.Value,
                    order.ShippedDate.Value,
                    order.ShipRegion == null ? "NULL" : order.ShipRegion);
            }
        }
Example #10
0
        public static void UpdateCustomer(string customerId, Customer newCustomer)
        {
            NorthwindEntities context = new NorthwindEntities();
            Customer customer = context.Customers.Where(x => x.CustomerID == customerId).FirstOrDefault();

            if (customer == null)
            {
                throw new ArgumentNullException("The customer you are looking for does not exist!");
            }

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

            context.SaveChanges();
        }
Example #11
0
 static void Main(string[] args)
 {
     NorthwindEntities context = new NorthwindEntities();
     context.Database.CreateIfNotExists();
 }