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 affor开发者_C百科d that, any suggestion without UnityOfWork
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.
精彩评论