public static int AddCategory(ProductCategory category)
        {
            int result = 0;

            try
            {
                using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer())
                {
                    database.Configuration.ProxyCreationEnabled = false;
                    category.ModifiedDate = DateTime.Now;
                    category.rowguid = new Guid();

                    database.ProductCategories.Add(category);

                    database.SaveChanges();

                    ProductCategory newCategory = database.ProductCategories.FirstOrDefault(c => c.ProductCategoryID == category.ProductCategoryID);
                    result = (newCategory == null) ? 0 : newCategory.ProductCategoryID;
                }
            }
            catch (Exception e)
            {
                if (e.InnerException is System.Data.SqlClient.SqlException)
                {
                    DatabaseFault dbf = new DatabaseFault
                    {
                        DbOperation = "Connect to database",
                        DbReason = "Exception accessing database",
                        DbMessage = e.InnerException.Message
                    };

                    throw new FaultException<DatabaseFault>(dbf);
                }
                else
                {
                    SystemFault sf = new SystemFault
                    {
                        SystemOperation = "Adding category",
                        SystemReason = "Exception adding category",
                        SystemMessage = e.Message
                    };

                    throw new FaultException<SystemFault>(sf);
                }
            }

            return result;
        }
        public static ProductCategory UpdateCategory(ProductCategory category)
        {
            ProductCategory result = null;

            try
            {
                using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer())
                {
                    database.Configuration.ProxyCreationEnabled = false;

                    ProductCategory categoryToUpdate = database.ProductCategories.FirstOrDefault(c => c.ProductCategoryID == category.ProductCategoryID);
                    if (categoryToUpdate == null)
                    {
                        categoryToUpdate = new ProductCategory()
                        {
                            ProductCategoryID = 0,
                            Name = category.Name,
                            IsActive = category.IsActive,
                            ModifiedDate = DateTime.Now,
                            rowguid = Guid.NewGuid()
                        };
                        database.ProductCategories.Add(categoryToUpdate);
                    }
                    else
                    {
                        categoryToUpdate.Name = category.Name;
                        categoryToUpdate.IsActive = category.IsActive;
                        categoryToUpdate.ModifiedDate = DateTime.Now;
                        categoryToUpdate.rowguid = Guid.NewGuid();
                    }

                    database.SaveChanges();
                    result = database.ProductCategories.FirstOrDefault(c => c.rowguid == categoryToUpdate.rowguid);
                }
            }
            catch (Exception e)
            {
                if (e.InnerException is System.Data.SqlClient.SqlException)
                {
                    DatabaseFault dbf = new DatabaseFault
                    {
                        DbOperation = "Connect to database",
                        DbReason = "Exception accessing database",
                        DbMessage = e.InnerException.Message
                    };

                    throw new FaultException<DatabaseFault>(dbf);
                }
                else
                {
                    SystemFault sf = new SystemFault
                    {
                        SystemOperation = "Updating category",
                        SystemReason = "Updating category",
                        SystemMessage = e.Message
                    };

                    throw new FaultException<SystemFault>(sf);
                }
            }
            return result;
        }
 public ProductCategory UpdateCategory(ProductCategory category)
 {
     return CategoryCore.UpdateCategory(category);
 }
        public static ProductCategory GetCategory(int categoryID)
        {
            ProductCategory categoryToGet = new ProductCategory();

            try
            {
                using (BicycleWorldDataModelContainer database = new BicycleWorldDataModelContainer())
                {
                    database.Configuration.ProxyCreationEnabled = false;

                    var categories = from category in database.ProductCategories
                                     where category.ProductCategoryID == categoryID
                                     select category;
                    categoryToGet = categories.FirstOrDefault();
                }
            }
            catch (Exception e)
            {
                if (e.InnerException is System.Data.SqlClient.SqlException)
                {
                    DatabaseFault dbf = new DatabaseFault
                    {
                        DbOperation = "Connect to database",
                        DbReason = "Exception accessing database",
                        DbMessage = e.InnerException.Message
                    };

                    throw new FaultException<DatabaseFault>(dbf);
                }
                else
                {
                    SystemFault sf = new SystemFault
                    {
                        SystemOperation = "Iterate through categories",
                        SystemReason = "Exception reading categories",
                        SystemMessage = e.Message
                    };

                    throw new FaultException<SystemFault>(sf);
                }
            }

            return categoryToGet;
        }
 public int AddCategory(ProductCategory category)
 {
     return CategoryCore.AddCategory(category);
 }