private void FixupnpSupplier(Supplier previousValue, bool skipKeys = false)
        {
            if (IsDeserializing)
            {
                return;
            }

            if (previousValue != null && previousValue.npProducts.Contains(this))
            {
                previousValue.npProducts.Remove(this);
            }

            if (npSupplier != null)
            {
                if (!npSupplier.npProducts.Contains(this))
                {
                    npSupplier.npProducts.Add(this);
                }

                SupplierID = npSupplier.SupplierID;
            }
            else if (!skipKeys)
            {
                SupplierID = null;
            }

            if (ChangeTracker.ChangeTrackingEnabled)
            {
                if (ChangeTracker.OriginalValues.ContainsKey("npSupplier")
                    && (ChangeTracker.OriginalValues["npSupplier"] == npSupplier))
                {
                    ChangeTracker.OriginalValues.Remove("npSupplier");
                }
                else
                {
                    ChangeTracker.RecordOriginalValue("npSupplier", previousValue);
                }
                if (npSupplier != null && !npSupplier.ChangeTracker.ChangeTrackingEnabled)
                {
                    npSupplier.StartTracking();
                }
            }
        }
        /// <summary>
        /// Save Suppliers use for both Insert and Update 
        /// </summary>
        /// <param name="Supplier">The supplier.</param>
        public bool SaveSupplier(Supplier supplier)
        {
            Supplier existingData = new Supplier();

            existingData = GetSupplierByName(supplier.Name);

            if (existingData != null && existingData.SupplierID != supplier.SupplierID)
            {
                if (existingData.SupplierID != 0)
                    return false;
            }

            existingData = GetSupplierByID(supplier.SupplierID);

            RemoveSupplierFromTheCache(existingData);

            if (existingData == null)
                _context.Suppliers.AddObject(supplier);
            else
            {
                existingData.Name = supplier.Name;
                existingData.Description = supplier.Description;
                existingData.AddressID = supplier.AddressID;

                if (!_context.IsAttached(existingData))
                    _context.Suppliers.Attach(existingData);
            }
            _context.SaveChanges();

            AddSupplierToCache(existingData);

            return true;
        }
        /// <summary>
        /// Add Supplier to cache 
        /// </summary>
        /// <param name="supplier">The Supplier Object</param>
        private void AddSupplierToCache(Supplier supplier)
        {
            if (CacheEnabled && supplier != null)
            {
                List<Supplier> suppliers = new List<Supplier>();

                if (CacheSuppliers != null && CacheSuppliers.Count > 0)
                    suppliers = CacheSuppliers;

                Supplier cacheSupplier = new Supplier();
                if (CacheSuppliers != null)
                    cacheSupplier = CacheSuppliers.FirstOrDefault(c => c.SupplierID == supplier.SupplierID);

                if (CacheSuppliers != null && CacheSuppliers.Count > 0)
                {
                    suppliers.Add(supplier);
                    CacheSuppliers = suppliers;
                }
            }
        }
        /// <summary>
        /// Get Supplier By name. Firstly look from the project cache and get from the database 
        /// </summary>
        /// <param name="supplierName">The supplier name</param>
        /// <returns>Supplier</returns>
        public Supplier GetSupplierByName(string supplierName)
        {
            if (string.IsNullOrEmpty(supplierName))
                return null;

            Supplier supplier = new Supplier();
            if (CacheSuppliers != null)
                supplier = CacheSuppliers.FirstOrDefault(c => c.Name == supplierName);

            if (supplier == null || supplier.SupplierID == 0)
            {
                supplier = _context.Suppliers.FirstOrDefault(c => c.Name == supplierName);
                AddSupplierToCache(supplier);
                return supplier;
            }
            return supplier;
        }
        /// <summary>
        /// Get Supplier By ID. Firstly look from the project cache and get from the database 
        /// </summary>
        /// <param name="supplierID">The supplier identifier</param>
        /// <returns>Supplier</returns>
        public Supplier GetSupplierByID(int supplierID)
        {
            if (supplierID == 0)
                return null;

            Supplier supplier = new Supplier();
            if (CacheSuppliers != null)
                supplier = CacheSuppliers.FirstOrDefault(c => c.SupplierID == supplierID);

            if (supplier == null || supplier.SupplierID == 0)
            {
                supplier = _context.Suppliers.FirstOrDefault(c => c.SupplierID == supplierID);
                AddSupplierToCache(supplier);
                return supplier;
            }
            return supplier;
        }
        /// <summary>
        /// Remove Specific Supplier  from the cache 
        /// </summary>
        /// <param name="supplier">The Supplier  Object</param>
        private void RemoveSupplierFromTheCache(Supplier supplier)
        {
            if (CacheEnabled && supplier != null)
            {
                Supplier cacheSupplier = new Supplier();
                if (CacheSuppliers != null)
                    cacheSupplier = CacheSuppliers.FirstOrDefault(c => c.SupplierID == supplier.SupplierID);

                if (cacheSupplier != null)
                {
                    List<Supplier> suppliers = new List<Supplier>();

                    if (CacheSuppliers != null && CacheSuppliers.Count > 0)
                        suppliers = CacheSuppliers;

                    suppliers.Remove(cacheSupplier);
                    CacheSuppliers = suppliers;
                }
            }
        }
        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (txtManufacture.Text.Length > 0)
            {
                Supplier supplier = new Supplier();

                //for Edit Data
                if (SupplierID.Length > 0)
                {
                    supplier.SupplierID = int.Parse(SupplierID);
                    //supplier.CreatedOn = Convert.ToDateTime(txtManufacture.Tag.ToString());
                }
                supplier.Name = txtManufacture.Text.ToString();
                //supplier.IsActive = Convert.ToBoolean(chkIsActive.EditValue);
                //supplier.UpdatedOn = DateTime.Now;

                //for New Data
                if (SupplierID.Length < 0)
                {
                    //supplier.CreatedOn = DateTime.Now;
                }

                ProductService.SaveSupplier(supplier);

                grdManufacture.DataSource = ProductService.GetAllSuppliers();

                ClearForm();
            }
            else
            {
                MessageBox.Show("Please, fill the supplier name up!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtManufacture.Focus();
            }
        }
        private void FixupnpSupplier(Supplier previousValue, bool skipKeys = false)
        {
            //if (IsDeserializing)
            //{
            //    return;
            //}

            //if (previousValue != null && previousValue.npProductVariants.Contains(this))
            //{
            //    previousValue.npProductVariants.Remove(this);
            //}

            //if (npSupplier != null)
            //{
            //    if (!npSupplier.npProductVariants.Contains(this))
            //    {
            //        npSupplier.npProductVariants.Add(this);
            //    }

            //    SupplierID = npSupplier.SupplierID;
            //}
            //else if (!skipKeys)
            //{
            //    SupplierID = null;
            //}

            //if (ChangeTracker.ChangeTrackingEnabled)
            //{
            //    if (ChangeTracker.OriginalValues.ContainsKey("npSupplier")
            //        && (ChangeTracker.OriginalValues["npSupplier"] == npSupplier))
            //    {
            //        ChangeTracker.OriginalValues.Remove("npSupplier");
            //    }
            //    else
            //    {
            //        ChangeTracker.RecordOriginalValue("npSupplier", previousValue);
            //    }
            //    if (npSupplier != null && !npSupplier.ChangeTracker.ChangeTrackingEnabled)
            //    {
            //        npSupplier.StartTracking();
            //    }
            //}
        }