This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm loading an entity using code like this:
using (IUnitOfWork unitOfWork = context.CreateUnitOfWork())
{
return unitOfWork.FindOne ...
}
Later, when I try to access a one-to-many relationship on this entity by calling Get on an EntityCollection, I get the error: "Cannot access a disposed object. Object name: 'UnitOfWork'"
If I use the EagerLoad attribute, it works fine, but if I don't want to use EagerLoad, do I have to keep the unitOfWork open for the entire scope of using the entity?
Thanks.
|
|
|
Hi Andrew, This is because the entity will be associated with the unit of work that you used inside your using statement however because you're accessing it outside the using statement (which will dispose of the uow when closing the scope of the using sttaement). Internally the Get on the EntityCollection will be trying to re-use the associated uow which is now disposed. Either you do need an EagerLoad (which will pull the collection in inside the using statemenet), extend the scope of your unit of work or attach the entity to a new unit of work (with the .Attach() method) and then perform the operation you need. I hope that helps, let me know if you have any other questions. Kind regards, John-Daniel Trask |
|
|
Thanks. What's your recommended way of using a UOW in an asp.net page? Do you try and keep the UOW scope as small as possible and use .Attach(), or do you generally just create one UOW for the entire scope of the request?
Cheers.
|
|
|
Hi Andrew, It varies depending on what the application is doing but generally we have built web solutions where we scope the UoW to the request. For more information on this, check this blog post on the topic: http://www.mindscape.co.nz/blog/index.php/2008/05/12/using-the-unit-of-work-per-request-pattern-in-aspnet-mvc/ I hope that helps, John-Daniel |
|
|
I tried to remove an item from EntityCollection and I got the same error. I used EagerLoad on this. Please help... |
|
|
It sounds like you did the remove after the unit of work (with which the parent entity and the removed entity were associated) had been disposed. You need to keep the unit of work around for as long as you want to operate on it. If it's not obvious where the unit of work is getting disposed, could you post a sample which reproduces the problem please, and we will take a look at it. |
|