If an object is still managed by its context you can detach it without any of its related objects, even if they have been previously loaded in memory.
To do that you can use the overload of CreateDetachedCopy
which only takes one argument - T entity
. Consider the following example:
(EntitiesModel context =
Car originalCar = context.Cars.FirstOrDefault();
Car detachedCar = context.CreateDetachedCopy(originalCar);
Debug.Assert(detachedCar.RentalOrders.Count == 0);
Car attachedCar = context.AttachCopy(detachedCar);
Debug.Assert(attachedCar.RentalOrders.Count == 0);
The Car entity has a reference property Categories
and a reference collection RentalOrders
. CreateDetachedCopy(T entity)
detaches the entity without any of its navigation properties - the reference collection RentalOrders
of detachedCar is empty
and its reference property Category
. You can then attach the detached car without its navigation properties.
On the other side if you are attaching a newly created object, or are making any changes to the detached object, you will have to manually assure that its references to related objects are not initialized.
I hope this helps. If you have more questions do not hesitate to post in our forums again.
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features
shipped with our latest release!