コード例 #1
0
        private static void AddCustomer(object[] clientData)
        {
            using (var context = new CustomerOrdersEntities())
            {
                string   firstName = (string)clientData[0];
                string   lastName  = (string)clientData[1];
                DateTime birthDate = Convert.ToDateTime(clientData[2]);

                //check for the same customers
                var isSameCustomerInDataBase = context.Customer
                                               .Any(x => x.FirstName == firstName && x.LastName == lastName &&
                                                    x.BirthDate == birthDate);

                if (!isSameCustomerInDataBase)
                {
                    context.Customer.Add(new Customer()
                    {
                        FirstName = (string)clientData[0],
                        LastName  = (string)clientData[1],
                        BirthDate = Convert.ToDateTime(clientData[2])
                    });

                    context.SaveChanges();
                }
            }
        }
コード例 #2
0
        private static void AddOrders(object[] clientData, DataGridView productParameters)
        {
            int customerID = GetCustomerID(clientData);

            using (var context = new CustomerOrdersEntities())
            {
                foreach (DataGridViewRow item in productParameters.Rows)
                {
                    object Cell1 = item.Cells[0].Value;
                    object Cell2 = item.Cells[1].Value;
                    object Cell3 = item.Cells[2].Value;

                    string productName = (string)Cell1;
                    int    quantity    = Convert.ToInt32(Cell2);
                    float  price       = Convert.ToSingle(Cell3);

                    //Check for same Orders if there arent, add new order to database
                    var areThereSameOrders = context.Orders
                                             .Any(x => x.ProductName == productName &&
                                                  x.Quantity == quantity &&
                                                  x.Price == price
                                                  );

                    if (!areThereSameOrders)
                    {
                        context.Orders.Add(new Orders()
                        {
                            ProductName = Cell1.ToString(),
                            Quantity    = Convert.ToInt32(Cell2),
                            Price       = Convert.ToSingle(Cell3),
                            ClientID    = customerID
                        });
                    }
                }

                context.SaveChanges();
            }
        }