【发布时间】:2015-12-07 08:03:43
【问题描述】:
我想一次更新表中的几条记录。
所以我有如下表
它为Field_Value_EN 和Field_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