【问题标题】:linq update problem with linq to enititeslinq to 实体的 linq 更新问题
【发布时间】:2011-11-09 15:09:03
【问题描述】:

您好,我有一个名为 products...with 列的表

             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 无法跟踪具有相同键的多个对象。

有没有人帮忙解决这个问题.....

非常感谢....

【问题讨论】:

标签: c# winforms linq linq-to-entities entity-framework-4.1


【解决方案1】:

这行

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);

【讨论】:

  • @eranga 之后我可以像这样附加...product1.Name = txtProductname.Text; product1.Description = txtProductdescription.Text; product1.Price = Convert.ToDecimal(txtProductPrice.Text); product1.categoryId = 类别类型; dbcontext.SaveChanges();
  • @user844360 附加实体时,EF 会检查它是否已经拥有具有相同主键的实体副本,因为 EF 只保留实体的一个副本。如果实体已加载,附加将失败。
【解决方案2】:

您收到错误是因为您尝试更新的产品已由实体框架加载。您正在创建 product 的新实例并分配现有的 product id

您可以使用dbcontext.products DbSet 的Local 属性来检索现有产品。

 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();

您应该考虑使用正确的命名约定

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    相关资源
    最近更新 更多