【问题标题】:updating multiple rows in a table at once using linq使用 linq 一次更新表中的多行
【发布时间】:2015-12-07 08:03:43
【问题描述】:

我想一次更新表中的几条记录。

所以我有如下表

它为Field_Value_ENField_Value_AR 每个Product_ID 加载30 个编辑器

如下图

这些是相关的模型类

AB_Product_vs_Field 模型类

public partial class AB_Product_vs_Field
{
    public string Product_ID { get; set; }
    public string Field_ID { get; set; }
    public string Field_Value_EN { get; set; }
    public string Field_Value_AR { get; set; }
}

ProductEdit 模型类

public class ProductEdit
{
    public string Product_ID { get; set; }
    public string ProductTypeID { get; set; }
    public string ProductCategoryID { get; set; }
    public string Subsidary_ID { get; set; }
    public string Field_ID { get; set; }
    public string ProductFieldNameEn { get; set; }
    public string ProductFieldNameAr { get; set; }
    public string ApprovalStatus { get; set; }
    public string Status { get; set; }
    public IList<AB_Product_vs_Field> ListProductFields { get; set; }
    public IList<AB_ProductTypeCategoryField> ListProductLables { get; set; }
}

提交详细信息后,我想更新所有相关行 AB_Product_vs_Field 表。

例如:假设我更新了 Product_ID = 10 记录,它应该更新所有与 Product_ID = 10

相关的行

所以我像这样创建了我的 linq 查询

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Product_Edit(ProductEdit product)
    {
        AB_Product_vs_Field insertproductvalue = new AB_Product_vs_Field();      
        var user = db.AspNetUsers.Find(User.Identity.GetUserId());


        if (ModelState.IsValid)
        {

            var listInsertproductvalue = new List<AB_Product_vs_Field>(); 

            for (int i = 0; i < product.ListProductFields.Count; i++)
            {
                listInsertproductvalue.Add(new AB_Product_vs_Field
                {
                    Product_ID = product.Product_ID,
                    Field_ID = product.ListProductFields[i].Field_ID,
                    Field_Value_EN = product.ListProductFields[i].Field_Value_EN,
                    Field_Value_AR = product.ListProductFields[i].Field_Value_AR
                }); 

            };

            db.Entry(listInsertproductvalue).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

但在这里我在db.Entry(listInsertproductvalue).State = System.Data.Entity.EntityState.Modified; 行中遇到以下错误

实体类型 List`1 不是当前模型的一部分 上下文。


编辑

然后我像这样更改了 LINQ 查询

        AB_Product_vs_Field insertproductvalue = new AB_Product_vs_Field();

        if (ModelState.IsValid)
        {

            for (int i = 0; i < product.ListProductFields.Count; i++)
            {
                insertproductvalue.Field_ID = product.Product_ID;
                insertproductvalue.Product_ID = product.ListProductFields[i].Field_ID;
                insertproductvalue.Field_Value_EN = product.ListProductFields[i].Field_Value_EN;
                insertproductvalue.Field_Value_AR = product.ListProductFields[i].Field_Value_AR;


            };
            db.Entry(insertproductvalue).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

但随后出现以下错误

存储更新、插入或删除语句影响了意外 行数 (0)。实体可能已被修改或删除 实体已加载。

【问题讨论】:

  • 我认为 db.Entry 期望单个对象不列出
  • 那么如何将其转换为列表
  • 你必须指定.State的值吗?
  • 听不懂你说什么
  • 我以为你在找this答案

标签: c# asp.net-mvc linq asp.net-mvc-4 razor


【解决方案1】:

只需尝试更新循环内的实体

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Product_Edit(ProductEdit product)
    {
        var user = db.AspNetUsers.Find(User.Identity.GetUserId());

        if (ModelState.IsValid)
        {
            for (int i = 0; i < product.ListProductFields.Count; i++)
            {
                var insertproductvalue = new AB_Product_vs_Field
                {
                    Product_ID = product.Product_ID,
                    Field_ID = product.ListProductFields[i].Field_ID,
                    Field_Value_EN = product.ListProductFields[i].Field_Value_EN,
                    Field_Value_AR = product.ListProductFields[i].Field_Value_AR
                }); 

                // Attach and set to modified
                db.Entry(insertproductvalue).State = System.Data.Entity.EntityState.Modified;
            };

            db.SaveChanges();

【讨论】:

  • 然后我收到以下错误The key field 'Product_ID' cannot have a value of null. A non-null value is required for the key fields defined on type 'AB_Product_vs_Field'.
  • 这意味着product.Product_ID的值为null。当您将产品模型传递给编辑方法时,您必须提供要编辑的产品的 ID。
  • 是的,我通过了,目前它在 GET 方法中硬编码为 Product_ID = "01";
  • 然后,在您的 GET 和 PUT 之间,您忘记复制 Product_ID。顺便说一句:我认为使用strings 作为ID 不是一个好主意。最好使用ints 或Guids。
  • @kez,那么你现在的问题是什么?是意外的空引用还是并发错误(“自加载实体以来,实体可能已被修改或删除。”)?无论哪种方式,这些都是不同的错误。已为您提供了原始问题的解决方案,因此请将其标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 2015-10-30
  • 2014-02-17
  • 2016-06-04
  • 1970-01-01
相关资源
最近更新 更多