Example #1
0
        private void customersSaveButton_Click(object sender, EventArgs e)
        {
            var companyNameArg = customerCompanyName.Text.Trim();
            var descriptionArg = customerDescription.Text.Trim();
            var passwordArg    = customerPassword.Text;

            try
            {
                if (customersSaveMode)
                {
                    customerToModify = new Customer
                    {
                        CompanyName = companyNameArg,
                        Description = descriptionArg,
                        Password    = passwordArg,
                    };
                    if (context.Customers.Any(c => c.CompanyName == companyNameArg))
                    {
                        showErrorMessageBox("Company name is already taken.");
                    }
                    else
                    {
                        context.Customers.Add(customerToModify);
                        showSuccessMessageBox("Customer added.");
                    }
                }
                else
                {
                    customerToModify.CompanyName          = companyNameArg;
                    customerToModify.Description          = descriptionArg;
                    customerToModify.Password             = passwordArg;
                    context.Entry(customerToModify).State = EntityState.Modified;
                    showSuccessMessageBox("Customer updated.");
                }
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                showErrorMessageBox();
                Console.WriteLine("Exception while saving customer: " + ex.Message);
            }

            customersGridView.Refresh();
            customersClear();
        }
 static void ShowProducts_NavigationProperties()
 {
     using (var ctx = new ProdContext())
     {
         Console.WriteLine("Available products:");
         foreach (Product p in ctx.Products)
         {
             ctx.Entry(p).Reference(pr => pr.Category).Load();
             if (null != p.Category)
             {
                 Console.WriteLine("{0} {1} belongs to category {2}", p.ProductID, p.Name, p.Category.Name);
             }
             else
             {
                 Console.WriteLine("{0} {1} does not belong to any category", p.ProductID, p.Name, p.Category.Name);
             }
         }
     }
 }