簡體   English   中英

更新產品的類別ID,而不是創建新ID

[英]updating category id for the product instead of creating new id

我有兩張桌子

產品

                product_id
                product_Name
                product_Price
                product_Description
                product_image
                category_id

另一張桌子

                 category
                 category_id
                 category_name
                 category_description

我有一個帶有三個文本框的表單(比如tbProductPrice,tbProductName,tbProductdescription)一個組合框(cbcategorytypes)兩個按鈕一個編輯,另一個是保存按鈕..我正在嘗試更新產品表以及category_id

當我單擊編輯按鈕時,類別名稱將加載到組合框中

當我們點擊保存按鈕時,文本框中的任何值都將在產品表中更新以及我已完成以下代碼的類別...

              using (var vareditcontext = new abcEntities())
            {
                pictureBox1.Enabled = true;
                pictureBox1.Visible = true;
                Image image = pictureBox1.Image;
                byte[] bit = null;

                bit = imageToByteArray(image);
                product1 pd = vareditcontext.product1.Where(p => p.product_Id == productid
                                     && p.category_Id == productcategoryid).First();


                string category = cbcategorytypes.Text;

                var c = new category { category_Name = category }; //problem at this line 

                pd.category = c;
                pd.product_Name = tbProductName.Text;
                decimal price = Convert.ToDecimal(tbProductPrice.Text);
                pd.product_Price = price;
                pd.product_Description = tbProductdescription.Text;
                pd.product_Image = bit;
                vareditcontext.SaveChanges();                 
                this.Close();
            }

當我點擊保存按鈕時,我得到了這樣的例外..

參數超出范圍異常 ..

我收到此錯誤,因為當我編輯並嘗試保存產品詳細信息以及類別名稱時,新類別名稱將存儲在數據庫中.....而不是更新當前的...

我怎么能糾正這個問題..我的意思是不存儲新項目我想將已經存在的類別設置為產品...

是否有可能與linq ....

任何人都會對此有所幫助..

非常感謝....

您需要從上下文加載類別,或者將您在運行中創建的類別附加到上下文。 否則EF假定您要創建和存儲新的。

...
var c = new category { category_Name = category };
vareditcontext.Attach(c);
pd.category = c;
...

在MSDN中,您可以閱讀有關附加和分離對象的更多信息。

我假設category_id是您的category實體的Key屬性。 如果只將category_name加載到組合框中,則實際需要從數據庫加載Category ,因為在將類別分配給產品時,EF必須知道鍵值:

product1 pd = vareditcontext.product1
    .Where(p => p.product_Id == productid
        && p.category_Id == productcategoryid)
    .First();

category c = vareditcontext.categories
    .Where(cat => cat.category_name == cbcategorytypes.Text)
    .First(); // let's hope the name is unique to pick not the wrong one

pd.category = c;
// ...
vareditcontext.SaveChanges();

您也可以只加載category_id,然后利用Dennis的方法創建存根類別並將其附加到上下文:

product1 pd = vareditcontext.product1
    .Where(p => p.product_Id == productid
        && p.category_Id == productcategoryid)
    .First();

int categoryid = vareditcontext.categories
    .Where(cat => cat.category_name == cbcategorytypes.Text)
    .Select(cat => cat.category_id)
    .First();

var c = new category { category_id = categoryid };
    // stub entity must have at least the key property set
vareditcontext.categories.Attach(c);

pd.category = c;
// ...
vareditcontext.SaveChanges();

如果要將product表中的category_id列作為外鍵屬性公開到product模型類中,則只需設置category_id

product1 pd = vareditcontext.product1
    .Where(p => p.product_Id == productid
        && p.category_Id == productcategoryid)
    .First();

int categoryid = vareditcontext.categories
    .Where(cat => cat.category_name == cbcategorytypes.Text)
    .Select(cat => cat.category_id)
    .First();

pd.category_id = categoryid;
// ...
vareditcontext.SaveChanges();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM