entitybreezehottowel

Retrieve all unsaved (detached) entities


I am creating new entities but leaving them detached because I want to attach and save them later.

manager.createEntity("Employee", null, EntityState.Detached)

How can I retrieve all the added but detached entities from my entity manager? That is the entities I added that are in the cache but have not been saved?


Solution

  • You can't ask an EntityManager for detached entities because they are ... detached.

    "Detached" means they don't belong to an EntityManager.

    It is generally not a good idea to be modifying detached entities. You'll discover they don't behave like attached entities. For example, none of their navigation properties work ... for the simple reason that navigation properties look for related entities in the same EntityManager and this detached entity doesn't have an EntityManager.

    I think you need to explain what motivated you to create these entities in a detached state. Why not leave them as "Added" (the default state)?

    Perhaps you're worried about saving them prematurely? We can talk about how to guard against that.

    Perhaps you're creating them but don't really want to save them until the user has made at least ONE change? We can talk about patterns to cover that.