locked
Make multiple copies of entity with relationship and detach it - keeping the related data? RRS feed

  • Question

  • Hi,

    I am passing a complex entity to a function which must make multiple copies of that entity, detach the copies from the context and then work with the copies in memory.

    Problem: when I detach the copy from the context, all references default to NULL. But I need the referenced data.

    So currently I copy every single member over to the in-memory copy (including the referenced data), this of course if a maintenance mayhem.

    Any more elegant (EF5 with .NET 4) solution to this?

    TIA,

    Tim

    Saturday, August 18, 2012 9:41 AM

Answers

  • Thank you for the article about cloning entity objects, it provided some useful hints. I read that cloning through serializing is a performance hog, so I am still copying every property to clone the object. Would be great if EF could provide a clone() method via code generation.

    As for the original problem: I solved it by eagerly loading the referenced data and later - before saving the newly created object - detaching the referenced entities which I did not need to be saved.

    Regards

    Tim

    • Marked as answer by Allen_MSDN Tuesday, August 28, 2012 1:23 AM
    Sunday, August 26, 2012 4:56 PM

All replies

  • Hi timvdh,

    Welcome to MSDN Forum.

    The new features in EF5 are as below.

    EF 5 includes a number of new features and bug fixes to the EF4.3 release. Most of the new features are only available in applications targeting .NET 4.5, see the Compatibility section for more details.

    • Enum support allows you to have enum properties in your entity classes.
    • Spatial data types can now be exposed in your model using the DbGeography and DbGeometry types.
    • The Performance enhancements that we recently blogged about.
    • Code First will now detect if you have LocalDb or SQL Express available for creating new databases. Visual Studio 2012 includes LocalDb, whereas Visual Studio 2010 includes SQL Express.
    • Code First will add tables to existing database if the target database doesn’t contain any of the tables from the model.

    The EF Designer in Visual Studio 2012 also has some new features:

    • DbContext code generation for new models means that any new models created using the EF Designer will generate a derived DbContext and POCO classes by default. You can always revert to ObjectContext code generation if needed. Existing models will not automatically change to DbContext code generation.
    • Multiple-diagrams per model allows you to have several diagrams that visualize subsections of your overall model. Shapes on the design surface can also have coloring applied.
    • Table-Valued functions in an existing database can now be added to your model.
    • Batch import of stored procedures allows multiple stored procedures to be added to the model during model creation.

    Based on your issue, here's an article I think can help you, please refer to it: Cloning the Entity object and all related children using the Entity Framework.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, August 20, 2012 2:03 AM
  • Hi Tim,

    Is this a lazy loading problem? My first thought is that the data you are missing would be lazy loaded, but because you are detaching it before accessing it the data is null.

    If that is the case then I think the link that Allen posted is about cloning and taking into account lazy loading.

    Alternatively you could eagerly or explicitly load the child data or even turn off lazy loading when doing the query that you are interested in.

    A brief article with code samples about various ways of doing the loading can be found here: http://msdn.microsoft.com/en-us/data/jj574232

    If that isn't your problem then some more information about what you are doing with some code might help to sort it out, you are using EF 5 with .NET 4 right?


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Monday, August 20, 2012 4:25 PM
  • Thank you for the article about cloning entity objects, it provided some useful hints. I read that cloning through serializing is a performance hog, so I am still copying every property to clone the object. Would be great if EF could provide a clone() method via code generation.

    As for the original problem: I solved it by eagerly loading the referenced data and later - before saving the newly created object - detaching the referenced entities which I did not need to be saved.

    Regards

    Tim

    • Marked as answer by Allen_MSDN Tuesday, August 28, 2012 1:23 AM
    Sunday, August 26, 2012 4:56 PM
  • Thank you Glenn, I was actually already eagerly loading the data, but the problem was that after cloning the entity object the referenced data disappeared when I detached the cloned object. So now I keep it attached but detach the refererenced data just before saving. That seems to work OK (because I do not manipulate the referenced data but it is in an added state after cloning and will therefore possibly produce duplicates).

    Regards

    Tim

    Sunday, August 26, 2012 5:03 PM
  • Hi timvdh,

    I'm glad to hear that you have solved the issue. Based on your suggestion about clone method, if possible, please submit the feature here - http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions, Microsoft listens customers voice carefully. : )

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Allen_MSDN Tuesday, August 28, 2012 1:23 AM
    Tuesday, August 28, 2012 1:23 AM