我正在使用 dapper,我想使用 Linq 来更新我正在尝试使用的一个表中名为 status 的单个字段。
public async Task<Int32> ProcessUnprocessedTransactions(IEnumerable<BomComponentData> items)
{
IEnumerable<BomComponentData> _itemstoBeProcesed = items.Where(w => w.Status == (int)BomStatus.Scanned);
foreach (BomComponentData item in _itemstoBeProcesed)
{
item.Status = (int)BomStatus.Completed;
}
return await database.UpdateAsync(_itemstoBeProcesed);
}
我的班级如下:
public class BomComponentData
{
public int Sequence { get; set; }
public string BOMShortName { get; set; }
public string OperationName { get; set; }
public long BomId { get; set; }
public long BOMLineID { get; set; }
public long StockItemID { get; set; }
public string BomLineType { get; set; }
public int Quantity { get; set; }
public long UnitID { get; set; }
public decimal? MultipleOfBaseUnit { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Barcode { get; set; }
public long ProductGroupID { get; set; }
public string ProductGroupCode { get; set; }
public int Status { get; set; }
public int BinLocation { get; set; }
public string BinName { get; set; }
public string UOM { get; set; }
public int CalculatedValue { get; set; }
public int BomPickedCount { get; set; }
public int TotalLeftTopick
{
get { return Quantity - BomPickedCount; }
}
public enum BomStatus
{
Listed=1,
Scanned=2,
Confirmed=3,
Processed=4,
Completed=4,
InVisible=5
}
public override string ToString()
{
return Code;
}
}
但如果我使用像上面这样的 foreach,它就不起作用。我确信它应该正确更新项目,但我认为因为我正在查看我的 foreach 中的单个项目和更新中的列表,所以它没有正确更新。
我要做的就是将项目标记为已完成并准备好转移,我是通过状态列和一个 int 枚举来完成的。
也许我错过了我的主键是什么的声明?
编辑 2
当我使用主键的键声明时,我得到以下信息:
未处理的异常:System.AggregateException:发生一个或多个错误。(约束
编辑 3
我已经设置了我班级的密钥,但正如你所见,我的数据库上有自动增量,但它仍然崩溃。插入应该如何处理?
编辑 4
例如,我按如下方式插入数据库。这不应该工作吗?
List<BomComponentData> _bomList = new List<BomComponentData>();
_bomList.Add(new BomComponentData { Sequence = 1, Barcode = "0000000001498", BinLocation = 23, BinName = "A", BOMShortName = "GYNE-TXX", OperationName = "Example Product", Code = "TB9175CEA", Name = "Tiburon Peri/Gynae Pack-10", Quantity = 1, UOM = "Each" });
await database.InsertAllAsync(_bomList,true);
我已经为可以正常工作的更新放置了标签键,但是当我尝试使用该键进行插入时,它并没有说约束错误,但更新有效。有人不知道我如何解决 Dapper contrib 中的插入和更新问题。