Linq to Sql 在同一事务中添加和删除
我有一个更新主详细信息记录的操作方法。当数据第一次从视图到达控制器时,我从数据库中选择以前的记录并调用存储库的删除方法。
public ActionResult sale(int id, SaleRecord _SaleRecord,int? PositionID)
{
SalesRepository _SalesRepository = new SalesRepository();
if (ModelState.IsValid)
{
List<long> prevSaleIDs = _SaleRecord.Items.Where(y => y.ActualSaleID > 0).Select(x => x.ActualSaleID).Distinct().ToList();
foreach (var SaleID in prevSaleIDs)
{
var dbData = _SalesRepository.GetSale(SaleID);
_SalesRepository.DeleteSaleItems(dbData.mktActualSaleItems.ToList());
_SalesRepository.DeleteActualSales(dbData);
}
-----
-----
}
将记录放入删除后,我通过调用 updateModel 准备新记录并将其放入插入
public ActionResult sale(int id, SaleRecord _SaleRecord, int? PositionID)
{
//as above
mktActualSale _Sales = new mktActualSale();
UpdateModel(_Sales, "_SaleRecord");
_Sales.mktActualSaleItems.AddRange(_SaleRecord.Items.Where(z => z.SalesQuantity != 0).Select(x => new mktActualSaleItem { SalesQuantity = x.SalesQuantity, SKUID = x.SKUID }));
_SalesRepository.Add(_Sales);
_SalesRepository.Save();
}
,但是当调用 save 时,它给出了异常,告诉我数据库中违反了唯一键约束。违反此约束的原因是先前的记录未从数据库中删除,它只是想在其之上添加另一条记录。我该如何解决这个问题
问候
i have an action method for update of master detail record. when data first reaches from view to controller, i select the previous records from db and call delete method of repository.
public ActionResult sale(int id, SaleRecord _SaleRecord,int? PositionID)
{
SalesRepository _SalesRepository = new SalesRepository();
if (ModelState.IsValid)
{
List<long> prevSaleIDs = _SaleRecord.Items.Where(y => y.ActualSaleID > 0).Select(x => x.ActualSaleID).Distinct().ToList();
foreach (var SaleID in prevSaleIDs)
{
var dbData = _SalesRepository.GetSale(SaleID);
_SalesRepository.DeleteSaleItems(dbData.mktActualSaleItems.ToList());
_SalesRepository.DeleteActualSales(dbData);
}
-----
-----
}
after putting the records for delete i prepare new record by calling updateModel and put it for insert
public ActionResult sale(int id, SaleRecord _SaleRecord, int? PositionID)
{
//as above
mktActualSale _Sales = new mktActualSale();
UpdateModel(_Sales, "_SaleRecord");
_Sales.mktActualSaleItems.AddRange(_SaleRecord.Items.Where(z => z.SalesQuantity != 0).Select(x => new mktActualSaleItem { SalesQuantity = x.SalesQuantity, SKUID = x.SKUID }));
_SalesRepository.Add(_Sales);
_SalesRepository.Save();
}
but when save is called, it gives me exception telling that unique key constraint is violated in db. the reason of this constraint being violated is previous record is not deleted from database and it simply wants to add another record on top of it. How can i solve this problem
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么为什么不在添加新记录之前调用
_SalesRepository.Save();
呢?更新:
但是更新并不是比删除和添加更好。
只需获取记录并使用 try catch 更新它们,然后如果一切正常,请保存。
希望这有帮助
So why don't call
_SalesRepository.Save();
before add new records ??Update:
But isn't better to make Update instead of delete and add.
Just get the records and update them all using try catch then if everything OK do save.
Hope this helped