Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6142119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:20:36+00:00 2026-05-23T18:20:36+00:00

The navigation properties on my entity are causing my deep clone to fail with

  • 0

The navigation properties on my entity are causing my deep clone to fail with the error:

“An object with the same key already exists in the ObjectStateManager”

Background:

Users want to be able to clone a parent record and all its associated child records. I’m able to clone the parent entity alone (with no errors) using this simple technique:

_context.Detach(currentParentEntity);
_context.AddToParentEntities(currentParentEntity);
_context.SaveChanges();

I found that solution and another working shallow clone technique (from diamandiev) here.

Since what I really need is a deep copy I’ve tried implementing the serialization cloning technique shown here, here and here. My calling code looks like this:

ParentEntity clonedParentEntity = (ParentEntity)DeepClone(currentParentEntity);
_context.Detach(currentParentEntity);
clonedParentEntity.EntityKey = null;
_context.AddToParentEntities(clonedParentEntity);
_context.SaveChanges();

This code only works when cloning a currentParentEntity with no child entities (referenced in navigation properties). If child entites exist I get the “object with the same key already exists” error. Why? How can I deep clone both a parent entity and it’s associated child entities then save the cloned record without any errors?

Thanks in advance.

EDIT: For the complete accepted answer read Ladislav Mrnka’s answer plus the comments.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:20:36+00:00Added an answer on May 23, 2026 at 6:20 pm

    If you really used serialization you cloned both parent and child entities – that is not your problem. Your problem is calling Detach because it will remove only single entity you are detaching (not its children). So the error is caused by adding children with same keys already tracked by the context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object defined by my entity framework model that has navigation properties,
I am querying an Entity( Applicant ) which has multiple navigation properties, need to
By default Navigation Properties in Entity Frameworks creates properties at both the end of
Does anyone know if it's possible to not use the navigation properties feature of
I have an entity A with a simple navigation property B . For any
I have an Entity object, Item, that looks like this.. public class Item() {
I have 4 entities which I've defined Navigation properties on like the following: internal
I have a problem updating a foreign key in an Entity Framework entity. I
During navigation of the java.lang.reflect.Method class I came across the method isBridge . Its
The navigation on the left menu in the below site uses CSS for mouseover

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.