English 中文(简体)
不为林克工作的最新声明
原标题:update statement not working for linq

我只是为了更新与分类表和产品表有关的表格而感到mad。

I have these tables

               product product_id
                       product_name 
                       product_description
                       product_price
                       product_image


              category  category_id
                        catgory_name
                        category_desc

我这样做是为了更新表格......利用实体框架......。

    private void btnSave_Click(object sender, EventArgs e)
    {

        if (lblHiddenmode.Text == "Edit")
        {
            using (var dbcontext = new TsgEntities())
            {
                pictureBox1.Enabled = true;
                pictureBox1.Visible = true;
                Image image = pictureBox1.Image;
                byte[] bit = null;

                bit = imageToByteArray(image);
                product1 pd = new product1();

                string category = cbcategorytypes.Text;
                string categorydesc = tbCategoryDescription.Text;

                var c = new category { category_Name = category, category_Description = categorydesc };

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

  }

NOTE:我正在更新产品名称,产品名称为4,类别为:id=4。

但它将在本发言中显示pd. category = c;。 i got 产品_id = “0”和“0”类

am i doing wrong when updating the table with category table .. is there any problem with update statement

最佳回答

以上准则将产生新内容,而不是更新。 为了进行更新,您首先需要检索product1。 您的发言。

我不知道你的情况如何,因此,我可以提出确切的法典,但这将是这样的:

product1 pd = dbcontext.protucts.Where(p => p.productid == 4 
                                         && p.categoryid == 4).First();

You can then make your changes and call dbcontext.SaveChanges() and it should update your record.

问题回答

You aren t actually adding your objects into your context.

You need to do something like:

dbcontext.products.AddObject(pd1);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签