public Customer Get(int id)
 {
     using (Context con = new Context())
     {
         return con.Customer.Include("Address").FirstOrDefault(x => x.Id == id);
     }
 }
 public bool Update(Customer item)
 {
     if (item == null)
         throw new ArgumentNullException("Customer is null");
     if (Get(item.Id) == null)
         return false;
     using (Context con = new Context())
     {
         if (con.Address.FirstOrDefault(x => x.Id == item.Address.Id) == null)
             throw new ArgumentException("You need to add the address to the database first: " + item.Address.StreetAddress);
         if (!con.Address.FirstOrDefault(x => x.Id == item.Address.Id).Equals(item.Address))
             throw new ArgumentException("You need to update the address first: " + item.Address.StreetAddress);
     }
     using (Context con = new Context())
     {
         var temp = con.Customer.Find(item.Id);
         con.Customer.Attach(temp);
         con.Address.Attach(item.Address);
         temp.Address = item.Address;
         con.Entry(temp).CurrentValues.SetValues(item);
         con.Entry(temp).State = EntityState.Modified;
         con.SaveChanges();
         return true;
     }
 }
 public IEnumerable<Customer> GetAll()
 {
     using (Context con = new Context())
     {
         return con.Customer.Include("Address").ToList();
     }
 }
 public IEnumerable<Ingredient> GetAll()
 {
     using (Context con = new Context())
     {
         return con.Ingredient.ToList();
     }
 }
 public Menu Get(int id)
 {
     using (Context con = new Context())
     {
         return con.Menu.Include("Dishes.Ingredients").FirstOrDefault(x => x.Id == id);
     }
 }
        public Party Add(Party item)
        {
            if (item == null)
                throw new ArgumentNullException("Party is null");
            using (Context con = new Context())
            {
                foreach (var ing in item.Menus)
                {
                    if (con.Menu.FirstOrDefault(x => x.Id == ing.Id) == null)
                        throw new ArgumentException("You need to add the menu to the database first: " + ing.Name);
                    if (!con.Menu.FirstOrDefault(x => x.Id == ing.Id).Equals(ing))
                        throw new ArgumentException("You need to update the menu first: " + ing.Name);
                }
                if (con.Address.FirstOrDefault(x => x.Id == item.Address.Id) == null)
                    throw new ArgumentException("You need to add the address to the database first: " + item.Address.StreetAddress);
                if (!con.Address.FirstOrDefault(x => x.Id == item.Address.Id).Equals(item.Address))
                    throw new ArgumentException("You need to update the address first: " + item.Address.StreetAddress);

                if (con.Customer.FirstOrDefault(x => x.Id == item.Customer.Id) == null)
                    throw new ArgumentException("You need to add the customer to the database first: " + item.Customer.FirstName);
                if (!con.Customer.FirstOrDefault(x => x.Id == item.Customer.Id).Equals(item.Customer))
                    throw new ArgumentException("You need to update the customer first: " + item.Customer.FirstName);
            }

            using (Context con = new Context())
            {
                item.Menus.ForEach(x => con.Menu.Find(x.Id));
                con.Address.Attach(item.Address);
                con.Customer.Attach(item.Customer);
                item = con.Party.Add(item);
                con.SaveChanges();
            }
            return item;
        }
 public Party Get(int id)
 {
     using (Context con = new Context())
     {
         return con.Party.Include("Menus.Dishes.Ingredients").Include("Customer.Address").Include("Address").FirstOrDefault(x => x.Id == id);
     }
 }
 public Ingredient Get(int id)
 {
     using (Context con = new Context())
     {
         return con.Ingredient.FirstOrDefault(x => x.Id == id);
     }
 }
        public Menu Add(Menu item)
        {
            if (item == null)
                throw new ArgumentNullException("Menu is null");
            using (Context con = new Context())
            {
                foreach (var ing in item.Dishes)
                {
                    if (con.Dish.FirstOrDefault(x => x.Id == ing.Id) == null)
                        throw new ArgumentException("You need to add the dish to the database first: " + ing.Name);
                    if (!con.Dish.FirstOrDefault(x => x.Id == ing.Id).Equals(ing))
                        throw new ArgumentException("You need to update the dish first: " + ing.Name);
                }

                var dishes = new List<Dish>();
                foreach (var dish in item.Dishes)
                {
                    dishes.Add(con.Dish.FirstOrDefault(y => y.Id == dish.Id));
                }
                item.Dishes.Clear();
                item.Dishes = dishes;
                con.Menu.Add(item);
                con.SaveChanges();
            }
            return item;
        }
        public IEnumerable<Party> GetAll()
        {
            using (Context con = new Context())
            {
                return con.Party.Include("Menus.Dishes.Ingredients").Include("Customer.Address").Include("Address").ToList();

            }
        }
        public IEnumerable<Menu> GetAll()
        {
            using (Context con = new Context())
            {
                return con.Menu.Include("Dishes.Ingredients").ToList();

            }
        }
        public Ingredient Add(Ingredient item)
        {
            if (item == null)
                throw new ArgumentNullException("Ingredient is null");

            using (Context con = new Context())
            {
                item =  con.Ingredient.Add(item);
                con.SaveChanges();
            }
            return item;
        }
 public void Remove(int id)
 {
     Customer cus = Get(id);
     if (cus == null)
         throw new ArgumentException("The customer did not exist in the db");
     using (Context con = new Context())
     {
         con.Customer.Attach(cus);
         con.Customer.Remove(cus);
         con.SaveChanges();
     }
 }
 public void Remove(int id)
 {
     Party party = Get(id);
     if (party == null)
         throw new ArgumentException("The party did not exist in the db");
     using (Context con = new Context())
     {
         con.Party.Attach(party);
         con.Party.Remove(party);
         con.SaveChanges();
     }
 }
 public void Remove(int id)
 {
     Ingredient ing = Get(id);
     if (ing == null)
         throw new ArgumentException("The ingredient did not exist in the db");
     using(Context con = new Context())
     {
         con.Ingredient.Attach(ing);
         con.Ingredient.Remove(ing);
         con.SaveChanges();
     }
 }
 public void Remove(int id)
 {
     Menu menu = Get(id);
     if (menu == null)
         throw new ArgumentException("The menu did not exist in the db");
     using (Context con = new Context())
     {
         con.Menu.Attach(menu);
         con.Menu.Remove(menu);
         con.SaveChanges();
     }
 }
 public bool Update(Ingredient item)
 {
     if (item == null)
         throw new ArgumentNullException("Ingredient is null");
     if (Get(item.Id) == null)
         return false;
     using (Context con = new Context())
     {
         con.Ingredient.Attach(item);
         con.Entry(item).State = System.Data.Entity.EntityState.Modified;
         con.SaveChanges();
         return true;
     }
 }
 public Customer Add(Customer item)
 {
     if (item == null)
         throw new ArgumentNullException("Customer is null");
     using (Context con = new Context())
     {
         if (con.Address.FirstOrDefault(x => x.Id == item.Address.Id) == null)
             throw new ArgumentException("You need to add the address to the database first: " + item.Address.StreetAddress);
         if (!con.Address.FirstOrDefault(x => x.Id == item.Address.Id).Equals(item.Address))
             throw new ArgumentException("You need to update the address first: " + item.Address.StreetAddress);
     }
     using (Context con = new Context())
     {
         con.Address.Attach(item.Address);
         item = con.Customer.Add(item);
         con.SaveChanges();
     }
     return item;
 }
 public bool Update(Menu item)
 {
     if (item == null)
         throw new ArgumentNullException("Menu is null");
     if (Get(item.Id) == null)
         return false;
     using (Context con = new Context())
     {
         foreach (var ing in item.Dishes)
         {
             if (con.Dish.FirstOrDefault(x => x.Id == ing.Id) == null)
                 throw new ArgumentException("You need to add the dish to the database first: " + ing.Name);
             if (!con.Dish.FirstOrDefault(x => x.Id == ing.Id).Equals(ing))
                 throw new ArgumentException("You need to update the dish first: " + ing.Name);
         }
     }
     using (Context con = new Context())
     {
         var temp = con.Menu.Find(item.Id);
         con.Menu.Attach(temp);
         con.Entry(temp).CurrentValues.SetValues(item);
         foreach (var ing in item.Dishes.ToList())
         {
             if (!temp.Dishes.Any(x => x.Id == ing.Id))
             {
                 con.Dish.Attach(ing);
                 temp.Dishes.Add(ing);
             }
         }
         foreach (var ing in temp.Dishes.ToList())
         {
             if (!item.Dishes.Any(x => x.Id == ing.Id))
             {
                 con.Dish.Attach(ing);
                 temp.Dishes.Remove(ing);
             }
         }
         con.Entry(temp).State = EntityState.Modified;
         con.SaveChanges();
         return true;
     }
 }
        public bool Update(Party item)
        {
            if (item == null)
                throw new ArgumentNullException("Party is null");
            if (Get(item.Id) == null)
                return false;
            using (Context con = new Context())
            {
                foreach (var ing in item.Menus)
                {
                    if (con.Menu.FirstOrDefault(x => x.Id == ing.Id) == null)
                        throw new ArgumentException("You need to add the menu to the database first: " + ing.Name);
                    if (!con.Menu.FirstOrDefault(x => x.Id == ing.Id).Equals(ing))
                        throw new ArgumentException("You need to update the menu first: " + ing.Name);
                }
                if (con.Address.FirstOrDefault(x => x.Id == item.Address.Id) == null)
                    throw new ArgumentException("You need to add the address to the database first: " + item.Address.StreetAddress);
                if (!con.Address.FirstOrDefault(x => x.Id == item.Address.Id).Equals(item.Address))
                    throw new ArgumentException("You need to update the address first: " + item.Address.StreetAddress);

                if (con.Customer.FirstOrDefault(x => x.Id == item.Customer.Id) == null)
                    throw new ArgumentException("You need to add the customer to the database first: " + item.Customer.FirstName);
                if (!con.Customer.FirstOrDefault(x => x.Id == item.Customer.Id).Equals(item.Customer))
                    throw new ArgumentException("You need to update the customer first: " + item.Customer.FirstName);
            }
            using (Context con = new Context())
            {
                //Changes party properties
                Party temp =  con.Party.Find(item.Id);
                con.Party.Attach(temp);
                con.Entry(temp).CurrentValues.SetValues(item);
                temp.Menus = con.Party.Include("Menus").FirstOrDefault(x => x.Id == item.Id).Menus.ToList();

                //New address
                if (!temp.Address.Equals(item.Address))
                {
                    con.Address.Attach(item.Address);
                    temp.Address = item.Address;
                }

                //New Customer
                if (!temp.Customer.Equals(item.Customer))
                {
                    con.Customer.Attach(item.Customer);
                    temp.Customer = item.Customer;
                }

                //Adds new menus
                foreach (var ing in item.Menus.ToList())
                {
                    if (!temp.Menus.Any(x => x.Id == ing.Id))
                    {
                        con.Menu.Attach(ing);
                        temp.Menus.Add(ing);
                    }
                }

                //Deletes removed menus
                foreach (var ing in temp.Menus.ToList())
                {
                    if (!item.Menus.Any(x => x.Id == ing.Id))
                    {
                        con.Menu.Attach(ing);
                        temp.Menus.Remove(ing);
                    }
                }

                //Saves changes
                con.Entry(temp).State = EntityState.Modified;
                con.SaveChanges();
                return true;
            }
        }