none
Cloning an entity, unable to detach - ObjectStateManager RRS feed

  • Question

  • i have been following the example detailed on this link

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

    but when i try to do exactly the same i get the error An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

     

    my code starts as follows

                using (var context = new OperateITNow_DEVEntities())
                {
                    try
                    {
                        //add new dateRange
                        DateRange dateRange = context.DateRange.First(d => d.DateRangeId == dateRangeId);
                        DateRange newDateRange = dateRange.Clone();
    
                        context.Detach(dateRange);
         
                        newDateRange.EntityKey = null;
                        newDateRange.DateRangeGUID = Guid.NewGuid();
                        newDateRange.LastModified = DateTime.Now;
    
                        context.AddToDateRange(newDateRange);
    


    i think the issue might be to do with the DateRangeID field within the cloned entity, this is an Identity, autoincrement field in a SQL table but i thought setting the entitykey to null would solve this?

    any ideas?

    Cheers,
    Craig

    Tuesday, December 13, 2011 10:14 PM

Answers

  • Hi Craig,

    Welcome to MSDN Forum!

    Based on the first post, I have tested the code and received the same exception message. After researching, I find the AddTonote has succeeded, but you can’t save the clone instance to the database when you call SaveChanges method. Then I look carefully at article again, I found at the end of the article, the author said” And no we have successfully cloned a record (Entity) and put a copy into the database. This would work very similarly for Linq-to-SQL, but I have not implemented it quite yet. So, it isn’t a worked solution at all.

    But I searched out another article which tells us how to Clone the Entity object and all related children using the Entity Framework. If you have interested in it, please refer the link

    Cloning the Entity object and all related children using the Entity Framework

    Best Regards

     


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Craig G Thursday, December 15, 2011 10:07 AM
    Thursday, December 15, 2011 3:54 AM
    Moderator

All replies

  • ok i think my issue is to do with related entity objects.

    DateRange has Rateset,Rate_New & OAR related entities. Would this be a cause & Is there a way to do a cascade detach?

    also, im using .net 4.0 so cant use AsNoTracking()

    • Edited by Craig G Wednesday, December 14, 2011 9:35 AM
    Wednesday, December 14, 2011 9:29 AM
  • ok if i detach the related objects i now get a different 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"


    The EntityKey is an auto increment identiity field. so setting it to null is causing the issue. but how do you set the EntityKey as an autoincrement of the DateRange ObjectContext?
    Wednesday, December 14, 2011 10:01 AM
  • Hi Craig,

    Welcome to MSDN Forum!

    Based on the first post, I have tested the code and received the same exception message. After researching, I find the AddTonote has succeeded, but you can’t save the clone instance to the database when you call SaveChanges method. Then I look carefully at article again, I found at the end of the article, the author said” And no we have successfully cloned a record (Entity) and put a copy into the database. This would work very similarly for Linq-to-SQL, but I have not implemented it quite yet. So, it isn’t a worked solution at all.

    But I searched out another article which tells us how to Clone the Entity object and all related children using the Entity Framework. If you have interested in it, please refer the link

    Cloning the Entity object and all related children using the Entity Framework

    Best Regards

     


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Craig G Thursday, December 15, 2011 10:07 AM
    Thursday, December 15, 2011 3:54 AM
    Moderator
  • nice one cheers for that!
    Thursday, December 15, 2011 10:07 AM