我正在尝试单独更新每个产品,其中可以在一个订单中找到多个产品。当我按下提交/更新时,我将 orderid 与新产品数量一起发送到 Controller 。这是我看到的错误:
System.Data.SqlClient.SqlException: New transaction is not allowed because there are other threads running in the session.
这是重定向到表单的 Controller 。
[HttpGet]
[Authorize(Roles = "ADM")]
public ActionResult UpdateDetails(Guid id)
{
BusinessLayer.Orders blorder = new BusinessLayer.Orders();
return View(blorder.GetOrderDetailsByOrderId(id));
}
这是表格。
@model IQueryable<CommonLayer.ORDERDETAIL>
@{
ViewBag.Title = "UpdateDetails";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>UpdateDetails</h2>
<table class="table table-striped" style="font-size:medium;">
<tr>
<th>
Order Details ID
</th>
<th>
Price
</th>
<th>
VAT Rate
</th>
<th>
Quantity
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.OrderDetailsId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductVATRate)
</td>
<td>
<form action="/order/updatedetails/@item.OrderDetailsId" method="post" enctype="multipart/form-data">
<input type="text" name="prodqty" value="@item.ProductQuantity" />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<br />
<input type="submit" value="Update" class="btn btn-primary" />
</div>
</div>
</form>
</td>
</tr>
}
</table>
这是表单重定向到的 Controller 。
[HttpPost]
[Authorize(Roles = "ADM")]
public ActionResult UpdateDetails(Guid id, int prodqty)
{
BusinessLayer.Orders blorder = new BusinessLayer.Orders();
CommonLayer.ORDERDETAIL orderdetail = blorder.GetOrderDetailByDetailId(id);
blorder.UpdateOrderDetails(orderdetail, prodqty);
return RedirectToAction("ViewOrder");
}
这是用于实际更新库存的代码。
public void UpdateOrderDetails(CommonLayer.ORDERDETAIL orderdetail, int prodqty)
{
CommonLayer.ORDERDETAIL orderDetails = this.GetOrderDetail(orderdetail.OrderDetailsOrderId);
CommonLayer.ORDERDETAIL od = new CommonLayer.ORDERDETAIL();
DataLayer.DAProducts daprod = new DataLayer.DAProducts(this.Entities);
CommonLayer.PRODUCT product = daprod.GetProduct(orderDetails.OrderDetailsProductId);
od.OrderDetailsProductId = orderDetails.OrderDetailsProductId;
od.OrderDetailsId = orderDetails.OrderDetailsId;
od.OrderDetailsOrderId = orderDetails.OrderDetailsOrderId;
od.ProductVATRate = orderDetails.ProductVATRate;
od.ProductPrice = orderDetails.ProductPrice;
od.ProductQuantity = prodqty;
if (od.ProductQuantity <= product.ProductQuantity)
{
if (od.ProductQuantity > orderDetails.ProductQuantity)
{
product.ProductQuantity -= (od.ProductQuantity - orderDetails.ProductQuantity);
daprod.UpdateProduct(product);
this.Entities.Entry(orderDetails).CurrentValues.SetValues(od);
this.Entities.SaveChanges();
}
else if (od.ProductQuantity < orderDetails.ProductQuantity)
{
product.ProductQuantity += (orderDetails.ProductQuantity - od.ProductQuantity);
daprod.UpdateProduct(product);
this.Entities.Entry(orderDetails).CurrentValues.SetValues(od);
this.Entities.SaveChanges();
}
}
else
{
throw new Exception("Stock is too low");
}
}
这就是在 this.Entities.SaveChanges();
public void UpdateProduct(CommonLayer.PRODUCT product)
{
CommonLayer.PRODUCT ExistingProduct = this.GetProduct(product.ProductId);
this.Entities.Entry(ExistingProduct).CurrentValues.SetValues(product);
this.Entities.SaveChanges();
}
我该如何解决这个问题?
最佳答案
哦哦。我认为您过多地重复使用实体。尝试为您使用的每个事务初始化一个新的上下文...执行此操作(同样,从 UpdateProduct
函数中删除 SaveChanges
,这样您将只调用 SaveChanges
一次...在你的 if-else-if
block 之前尝试这个
using(var context = new CommonLayer.DBModelEntities()){
if (od.ProductQuantity > orderDetails.ProductQuantity)
{
product.ProductQuantity -= (od.ProductQuantity - orderDetails.ProductQuantity);
daprod.UpdateProduct(product, context);
context.Entry(orderDetails).CurrentValues.SetValues(od);
}
else if (od.ProductQuantity < orderDetails.ProductQuantity)
{
product.ProductQuantity += (orderDetails.ProductQuantity - od.ProductQuantity);
daprod.UpdateProduct(product, context);
context.Entry(orderDetails).CurrentValues.SetValues(od);
}
context.SaveChanges();
}
有了这个,您还应该更改 UpdateProduct
函数以接收您正在使用的当前数据库上下文
public void UpdateProduct(CommonLayer.PRODUCT product, CommonLayer.DBModelEntities context)
{
CommonLayer.PRODUCT ExistingProduct = this.GetProduct(product.ProductId);
context.Entry(ExistingProduct).CurrentValues.SetValues(product);
}
关于c# - 错误: System. Data.SqlClient.SqlException:不允许新事务,因为 session 中有其他线程在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35400351/