实体对象不能被 IEntityChangeTracker 的多个实例引用
大家好,我正在使用 EF4 处理 MVC2 项目,但遇到以下异常:
一个实体对象不能被 IEntityChangeTracker 的多个实例引用
我正在尝试执行以下操作:
Transaction transaction = new Transaction();
transaction.Amount = response.Amount;
...
_transactionService.Add(transaction);
_transactionService.Save();
OrderPayment orderPayment = new OrderPayment();
orderPayment.AuthorizationTransaction = transaction;
...
_orderPaymentService.AddOrderPayment(orderPayment);
_orderPaymentService.Save();
我有 3 层
- 的基本 CRUD
- 一个存储库层,其中我拥有所有 EF4 逻辑,以及每个实体A 应用我所有业务逻辑并使用存储库的服务层,当然,我在这里没有对 EF4 的对象上下文
- 以及我的网络中的 MVC 内容 的任何引用上面的代码
属于Web层的控制器,我的存储库是:
OrderPaymentRepo
public void AddOrderPayment(OrderPayment orderPayment)
{
_pharmacyDpnCtx.OrderPayments.AddObject(orderPayment);
}
public int Save()
{
return _pharmacyDpnCtx.SaveChanges();
}
和TransactionRepo:
public void Add(Transaction transaction)
{
_pharmacyDpnCtx.Transactions.AddObject(transaction);
}
public int Save()
{
return _pharmacyDpnCtx.SaveChanges();
}
我正在网络上进行研究,但很多解决方案都有UnityOfWork 使用相同的ObjectContext,这是最好的解决方案,但现在我可以负担得起,任何没有 UnityOfWork 的建议
hey guys, i am working in a MVC2 project with EF4 and i am having the following exception:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker
i am trying to do something like this:
Transaction transaction = new Transaction();
transaction.Amount = response.Amount;
...
_transactionService.Add(transaction);
_transactionService.Save();
OrderPayment orderPayment = new OrderPayment();
orderPayment.AuthorizationTransaction = transaction;
...
_orderPaymentService.AddOrderPayment(orderPayment);
_orderPaymentService.Save();
i have 3 layers
- A repository layer where i have all the EF4 logic, and the basic CRUD for each entity
- A service layer that apply all my business logic and uses the repo, of course, i dont have in here any reference to the objectcontext of the EF4
- And the MVC stuff in my web layer
the above code belongs to a controller of the web layer, and my repos are:
OrderPaymentRepo
public void AddOrderPayment(OrderPayment orderPayment)
{
_pharmacyDpnCtx.OrderPayments.AddObject(orderPayment);
}
public int Save()
{
return _pharmacyDpnCtx.SaveChanges();
}
and the TransactionRepo:
public void Add(Transaction transaction)
{
_pharmacyDpnCtx.Transactions.AddObject(transaction);
}
public int Save()
{
return _pharmacyDpnCtx.SaveChanges();
}
i was researching in the web but a lot of solutions has the UnityOfWork to use the same ObjectContext, that is the best solution but now i can afford that, any suggestion without UnityOfWork
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,看一下这个链接 - 8 个实体框架陷阱,在第五章和第六章,对你的问题有完整的解释。
其次,您可以做的最简单的事情是在 Save 和 Add 方法中使用相同的“Context”对象。
First, take a look at this link - 8 Entity Framework Gotchas, at the 5th and 6th chapter, there's a full explanation about your question.
Second, the easy thing you can do is to use the same "Context" object in the Save and Add method.