所以我正在研究这个实体框架项目,这个项目将被用作DAL,当运行压力测试(通过Thread()启动实体的几个更新)时,我得到这些:
_innerException = {“Transaction (Process ID 94) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.”}
以下是我如何实现我的类的方法的一些例子:
public class OrderController { public Order Select(long orderID) { using (var ctx = new BackEndEntities()) { try { var res = from n in ctx.Orders .Include("OrderedServices.Professional") .Include("Agency") .Include("Agent") where n.OrderID == orderID select n; return res.FirstOrDefault(); } catch (Exception ex) { throw ex; } } } public bool Update(Order order) { using (var ctx = new BackEndEntities()) { try { order.ModificationDate = DateTime.Now; ctx.Orders.Attach(order); ctx.SaveChanges(); return true; } catch (Exception ex) { throw ex; } } } }
和:
public class AgentController { public Agent Select(long agentID) { using (var ctx = new BackEndEntities()) { try { var res = from n in ctx.Agents.Include("Orders") where n.AgentID == agentID select n; return res.FirstOrDefault(); } catch (Exception ex) { throw ex; } } } public bool Update(Agent agent) { using (var ctx = new BackEndEntities()) { try { agent.ModificationDate = DateTime.Now; ctx.Agents.Attach(agent); ctx.ObjectStateManager.ChangeObjectState(agent,System.Data.EntityState.Modified); ctx.SaveChanges(); return true; } catch (Exception ex) { throw ex; } } } }
显然,这里的代码可能会更好,但我更喜欢EF新手.但我认为我的问题是上下文的设计问题.
我记得有人在这里提到,如果我的上下文不共享,我不会遇到这些死锁问题.
这对我来说似乎并不“共享”,因为我在每个方法中使用新的BackEndEntities(),所以我必须改变什么才能使其更加健壮?
这个DAL将用于在互联网上暴露的Web服务(在coure的代码审查之后),所以我无法控制多少会受到压力,许多不同的实例可能想要更新同一个实体.
谢谢!