locked
Cannot clone an entity. Tried various ways, here are the problems I faced.. RRS feed

  • Question

  • I am using Entity Framework 4 and Visual Studio 10 - c#.

    Background on what the application is:

    I created an application strongly typed ObjectContext class and Self Tracking POCO Entities.

    I update the conceptual model from the database.

    Now I have a class called "Initiative" which has many relationships with other entity collections. Atleast 8 relationships with 8 tables.

    Primary key of the Initiative is InitI.

    Now I load a saved Initative in the application, and click on 'copy' when a dialog box opens and I specify a new Intiative Title, rest all data is to be copied then I click save.

    Here are the different ways A. B. C. below I was trying to do the copy, and the errors that I get:

    A.

      //I serialized the object and created a clone and assigned to another Intiative variable.

    cloneInit = _miModel.CloneProxy(oInit);

    //Tried to add the clonedInitiative to the database, hoping it will detect it as a new object and assign to it an InitI

    _miModel.PublicContext.AddObject("Initiatives", cloneInit);

    Error: The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object.

     

    B .

    //I create a brand new initiative
    var brandNewInit = new Initiative()

    //And I save it, and I get an InitI
    int newId = _miModel.Add(brandNewInit);

    //Now I try to copy all fields of the source initiative to the target initiative
    brandNewInit = _toInit;

    //and Now I try to assign back the InitI (newId) that I received first when I added a blank brandNewInit, so that I can update the changes.
    brandNewInit.InitI = newId; //This is where I get the ERROR.

    //And then I try to save
    _miModel.Save();


    Error: //Exception Source: System.Data.Entity

    //Exception Type: System.InvalidOperationException

    //Exception Message: The property 'InitI' is part of the object's key information and cannot be modified.

    //Exception Target Site: VerifyEntityValueIsEditable

     

    C. This method I am trying as per the link

    http://naspinski.net/post/Cloning-an-Entity-in-Linq-to-Entities.aspx

    In this link I see that they have theEntityKey property. I dont have that property in my entities. How can I get that property in my entities?

    new_n.EntityKey = null;

     

    Is there a way to clone my entities and save to database?

    Been trying for this for too long now. All gurus please help.

    Thanks.

    --Jason

     

     

     

     


    Jlo
    Tuesday, November 16, 2010 1:35 PM

Answers

All replies

  • Hello Jason,

    Thanks for visiting ADO.NET Entity Framework forum!

    "How can I get that property in my entities?"

    Please try to check the target framework of your project in property page and make sure you add reference to System.Data.dll.

    Is there a way to clone my entities and save to database?

    Yes. One way is to use a Serializer clone the entity as shown here:
    http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/a967b44b-c85c-4afd-a499-f6ff604e2139/

    A much more deep cloner (include cloning the whole entity graph) is to use ILGenerator.Emit and ExpressionTree in reflection but there is a little bit more code:
    http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx

    Instead of cloning the entity, an alternative way is to create a new entity and assign each property manually.

    Best regards,


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Wednesday, November 17, 2010 7:29 AM
  • Hello,

    I'm writing to check the status of this thread. Please let me know if you need more help when you feel free. Thanks a lot!

    Have a good day!


    Best Regards,
    Roahn Luo
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Thursday, November 25, 2010 8:09 AM