New Posts New Posts RSS Feed: Unit Of Work not returning newly created Entity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Unit Of Work not returning newly created Entity

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: Unit Of Work not returning newly created Entity
    Posted: 25-Sep-2013 at 4:48pm
When a new Item is added in Cocktail, a new unit of work is created and added to the UnitOfWorkManager:

_unitOfWork = UnitOfWorkManager.Create();
ContactEntity contactEntity = await unitOfWork.ContactFactory.CreateAsync();
UnitOfWorkManager.Add(contactEntity.Id, _unitOfWork);



Then later, when the DetailViewModel is Starting, it finds the correct UnitOfWork, and then retrieves the newly created entity:

ContactEntity = await UnitOfWork.ContactEntities.WithIdAsync(contactId);

The issue I am having is that the above line throws an error, as the entity is not found.

I have stepped through the code carefully, and it retrieves the UnitOfWork for the correct id (-100) but somehow the entity is not present. So I don't see what is going wrong.

The Factory.CreateAsync is creating the instance and adding it to the EntityManager, so it should be able to find it again.

Any idea what's going on?

Greg            
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 25-Sep-2013 at 5:12pm
One thing to be aware of is that the UoWManager is a WeakDictionary, which means that unless something outside of the UoWManager is holding a reference to an item in the dictionary, the item will get garbage collected. The UoWManager will simply create a new UoW instance if the one with id -100 got garbage collected in the meantime, which is what you may be running into here. 
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 25-Sep-2013 at 6:17pm
Thx Marcel, got this resolved.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down