Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq到ENITES的linq更新问题_C#_Winforms_Linq_Linq To Entities_Entity Framework 4.1 - Fatal编程技术网

C# linq到ENITES的linq更新问题

C# linq到ENITES的linq更新问题,c#,winforms,linq,linq-to-entities,entity-framework-4.1,C#,Winforms,Linq,Linq To Entities,Entity Framework 4.1,您好,我有一个名为products的表…带有列 product_id (p.K) product_name product_description product_price category_id (F.k) 我有另一个桌子类别 category_id (p.k) category_

您好,我有一个名为products的表…带有列

             product_id  (p.K)
             product_name
             product_description
             product_price
             category_id  (F.k)
我有另一个桌子类别

                   category_id  (p.k)
                   category_name
我尝试的是用category_id更新产品表。我遇到了问题 我有以下代码

         Private void btnsave_click(object sender , eventargs e)
         {

                 if (datagridview1.SelectedRows.Count > 0)
                 {
                     int updateproductid = Convert.ToInt32(datagridview1.SelectedRows[0].Cells[0].Value);

                     string productcategories =cbCategorytypes.Text;

                     var categorytypes = (from producttype in dbcontext.categories
                                          where producttype.name.Equals(productcategories)
                                          select producttype.categoryId).SingleOrDefault();

                     product product1 = new product() { productId = updateproductid };
                     dbcontext.products.Attach(product1);
                     product1.Name = txtProductname.Text;
                     product1.Description = txtProductdescription.Text;
                     product1.Price = Convert.ToDecimal(txtProductPrice.Text);
                     product1.categoryId = categorytypes;
                     dbcontext.SaveChanges();
                 }

         }
获取错误:未处理无效操作异常:ObjectStateManager中已存在具有相同密钥的对象。ObjectStateManager无法跟踪具有相同密钥的多个对象

有人能帮我吗

非常感谢……

这句话

product product1 = new product() { productId = updateproductid };
dbcontext.products.Attach(product1);
告诉我您正在创建新产品并将其附加到上下文。但是这个产品已经存在了。您应该基于updateproductid检索产品,并设置新的categoryId或要更改的属性

更确切地说,你应该更换

product product1 = new product() { productId = updateproductid };
dbcontext.products.Attach(product1);
用这样的东西

product product1 = (from product in dbcontext.products 
                    where productId == updateproductid select product);
这条线

product product1 = new product() { productId = updateproductid };
dbcontext.products.Attach(product1);
告诉我您正在创建新产品并将其附加到上下文。但是这个产品已经存在了。您应该基于updateproductid检索产品,并设置新的categoryId或要更改的属性

更确切地说,你应该更换

product product1 = new product() { productId = updateproductid };
dbcontext.products.Attach(product1);
用这样的东西

product product1 = (from product in dbcontext.products 
                    where productId == updateproductid select product);

出现错误是因为您尝试更新的产品已由entity framework加载。您正在创建产品的新实例并分配现有产品id

可以使用dbcontext.products数据库集的属性检索现有产品

 int updateproductid = Convert.ToInt32(datagridview1.SelectedRows[0].Cells[0].Value);

 string productcategories =cbCategorytypes.Text;

 var categorytypes = (from producttype in dbcontext.categories
                      where producttype.name.Equals(productcategories)
                      select producttype.categoryId).SingleOrDefault();

 product product1 = dbcontext.products.Local.Where(p => p.productId == updateproductid).First();
 product1.Name = txtProductname.Text;
 product1.Description = txtProductdescription.Text;
 product1.Price = Convert.ToDecimal(txtProductPrice.Text);
 product1.categoryId = categorytypes;
 dbcontext.SaveChanges();

您应该考虑使用适当的命名约定

您正在获得错误,因为您要更新的产品已经由实体框架加载。您正在创建产品的新实例并分配现有产品id

可以使用dbcontext.products数据库集的属性检索现有产品

 int updateproductid = Convert.ToInt32(datagridview1.SelectedRows[0].Cells[0].Value);

 string productcategories =cbCategorytypes.Text;

 var categorytypes = (from producttype in dbcontext.categories
                      where producttype.name.Equals(productcategories)
                      select producttype.categoryId).SingleOrDefault();

 product product1 = dbcontext.products.Local.Where(p => p.productId == updateproductid).First();
 product1.Name = txtProductname.Text;
 product1.Description = txtProductdescription.Text;
 product1.Price = Convert.ToDecimal(txtProductPrice.Text);
 product1.categoryId = categorytypes;
 dbcontext.SaveChanges();

您应该考虑使用适当的命名约定

应该将主键设置为It=真,种子= 1,在哪里设置这些……在数据库中的列的属性中。主键应设置为identity=true,seed=1在数据库列的属性中,我在何处设置了这些键@然后我可以像这样附加…product1.Name=txtProductname.Text;product1.Description=txtProductdescription.Text;product1.Price=Convert.ToDecimaltxtProductPrice.Text;product1.categoryId=类别类型;dbcontext.SaveChanges@user844360当您附加实体时,EF会检查它是否已经具有具有相同主键的实体副本,因为EF只保留实体的一个副本。如果加载实体,则附加将失败。@eranga在此之后,我可以这样附加…product1.Name=txtProductname.Text;product1.Description=txtProductdescription.Text;product1.Price=Convert.ToDecimaltxtProductPrice.Text;product1.categoryId=类别类型;dbcontext.SaveChanges@user844360当您附加实体时,EF会检查它是否已经具有具有相同主键的实体副本,因为EF只保留实体的一个副本。如果加载实体,则连接将失败。