我正在使用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;
}
}但是,如果我像上面那样使用一个预言,它就不起作用了。我确信它应该正确地更新项目,但是我认为,由于我正在经历我的前文中的单数项和更新中的列表,所以更新是不正确的。
我所要做的就是将这些项目标记为已完成并准备好进行传输,我将通过status列和int枚举进行标记。
也许我错过了一个声明,说明什么是我的主要钥匙?
编辑2
当我使用主键的键声明时,会得到以下内容:
未处理的异常: System.AggregateException:发生了一个或多个错误。(约束)
编辑3
我已经设置了类的键,但正如您所见,我的db上有自动增量,而且它仍然崩溃。插入应如何处理?

编辑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控制下的插入和更新。
发布于 2019-05-26 20:27:54
您正在使用Dapper.Contrib,这个扩展要求使用一些属性来修饰类,以帮助自动处理数据。
特别是对于更新方法,需要定义Table属性和Key属性来标识主键
[Table ("BomComps")]
public class BomComponentData
{
// [ExplictKey]
[Key]
public int Sequence { get; set; }
....例如,在这里,我添加了属性表以在数据库中设置一个可能的表名,但是如果物理表的名称与类的名称匹配,则可以省略该属性。此外,我还添加了Key属性来设置包含表主键的属性(因此更新记录的语句可以用适当的WHERE条件形成)。
注意,如果列的Identity属性设置为yes,则应该使用Key属性;如果没有,则使用ExplicitKey属性来指示主键是由代码定义的。
发布于 2019-05-27 14:30:37
实际上,这是我必须解码我的类的问题,下面把这个留在这里,这样其他人就会有问题,我正在使用pcl libary,但是出于某种原因,dapper contribe没有检测到它必须按照以下方式声明的关键元素。
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public class StockTransferArgs
{
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int StockTransferArgsId { get; set; }
public long StockItemID { get; set; }
public string Code { get; set; }
public string OperationName { get; set; }
public string Description { get; set; }
public decimal Quantity { get; set; }
public long SourceWarehouseID { get; set; }
public string SourceBinName { get; set; }
public long TargetWarehouseID { get; set; }
public string TargetBinName { get; set; }
public string Reference { get; set; }
public string SecondReference { get; set; }
public string BarCode { get; set; }
public int Status { get; set; }
}https://stackoverflow.com/questions/56316719
复制相似问题