public FmThongTinSanPham(SanPham sp = null, SanPhamController spc = null)
        {
            InitializeComponent();
            dtgvHinhAnh.AutoGenerateColumns = false;
            dtgvChiTietSanPham.AutoGenerateColumns = false;
            //set primary context
            if (spc != null) SPCtr = spc;
            else SPCtr = new SanPhamController();

            HSXCtr = new HangSXController(this.SPCtr._db);
            MSCtr = new MauSacController(this.SPCtr._db);
            HACtr = new HinhAnhController(this.SPCtr._db);
            SPCTCtr = new SanPham_ChiTietController(this.SPCtr._db);
            MSCtr.timkiem();
            LoadCb_HangSX();
            if (sp == null)
            {
                this.sp = new SanPham(SPCtr._db);
                this.Text = "Thêm Mới Sản Phẩm";
            }
            else
            {
                this.sp = sp;
                this.sp._set_context(SPCtr._db);
                EditMode = true;
                ThongTinFormSanPham = this.sp;
                LoadDTGV_HinhAnh();
                LoadDTGV_ChiTietSP();
            }
        }
        public Boolean delete(SanPham obj)
        {
            try
            {
                //reload again to prevent error
                obj = get_by_id(obj.id);

                // remove relations
                while (obj.ds_sanpham_chitiet.Count > 0)
                {
                    SanPham_ChiTiet spct = obj.ds_sanpham_chitiet[0];
                    spct._set_context(obj._ctr._db);
                    spct.delete();
                }

                while (obj.ds_hinhanh.Count > 0)
                {
                    HinhAnh ha = obj.ds_hinhanh[0];
                    ha._set_context(obj._ctr._db);
                    ha.delete();
                }

                this._db.ds_sanpham.Remove(obj);
                //commit
                return save();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                return false;
            }
        }
 public int add(SanPham obj)
 {
     //call add
     this._db.ds_sanpham.Add(obj);
     save();
     return this._db.ds_sanpham.Max(x => x.id);
 }
 //method
 public void change(HinhAnh ha)
 {
     duongdan = ha.duongdan;
     duongdan_thumb = ha.duongdan_thumb;
     macdinh = ha.macdinh;
     if(ha.sanpham != null) sanpham = ha.sanpham;
 }
 public void change(SanPham sp)
 {
     masp = sp.masp;
     ten = sp.ten;
     mota = sp.mota;
     gia = sp.gia;
     if (active != sp.active)
     {
         active = sp.active;
         _set_active(active);
     }
     hangsx = sp.hangsx;
 }
 public List<String> validate(SanPham obj)
 {
     //
     List<String> re = new List<string>();
     //check
     if (!can_use_masp(obj.id, obj.masp))
     {
         re.Add("masp_exist_fail");
     }
     return re;
 }
        public void reload(SanPham sp)
        {
            List<HinhAnh> Added_HA = new List<HinhAnh>();
            List<HinhAnh> Deleted_HA = _db
                .ChangeTracker.Entries<HinhAnh>()
                .Where(en => en.Entity.sanpham == null && en.Entity.source_picture_from_web)
                .Select(t => t.Entity).ToList<HinhAnh>();

            List<SanPham_ChiTiet> Added_SPCT = new List<SanPham_ChiTiet>();
            List<SanPham_ChiTiet> Deleted_SPCT = _db
                .ChangeTracker.Entries<SanPham_ChiTiet>()
                .Where(en => en.Entity.sanpham == null && en.Entity.id > 0).Select(t => t.Entity)
                .ToList<SanPham_ChiTiet>();

            List<MauSac> Added_MauSac = _db.ChangeTracker.Entries<MauSac>().Where(ms => ms.State == EntityState.Added).Select(t => t.Entity).ToList();

            foreach (HinhAnh ha in sp.ds_hinhanh)
            {
                if (!ha.source_picture_from_web)
                {
                    Added_HA.Add(ha);
                }
                else
                {
                    //reload modified entity
                    _db.Entry<HinhAnh>(ha).Reload();
                }
            }

            foreach (SanPham_ChiTiet spct in sp.ds_sanpham_chitiet)
            {
                if (spct.id == 0)
                {
                    Added_SPCT.Add(spct);
                }
                else
                {
                    //reload modified entity
                    _db.Entry<SanPham_ChiTiet>(spct).Reload();
                }
            }

            //remove added entity
            foreach (HinhAnh ha in Added_HA)
            {
                _db.Entry<HinhAnh>(ha).State = EntityState.Detached;
                sp.ds_hinhanh.Remove(ha);
            }

            foreach (SanPham_ChiTiet spct in Added_SPCT)
            {
                _db.Entry<SanPham_ChiTiet>(spct).State = EntityState.Detached;
                sp.ds_sanpham_chitiet.Remove(spct);
            }

            foreach (MauSac ms in Added_MauSac) _db.Entry<MauSac>(ms).State = EntityState.Detached;

            //reload deleted entity
            foreach (HinhAnh ha in Deleted_HA) _db.Entry<HinhAnh>(ha).Reload();
            foreach (SanPham_ChiTiet spct in Deleted_SPCT) _db.Entry<SanPham_ChiTiet>(spct).Reload();
        }