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

  • Home
  • SEARCH
  • 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 6159855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:12:07+00:00 2026-05-23T21:12:07+00:00

I am getting an Error An object with the same key already exists in

  • 0

I am getting an Error “An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.“

public class TestObject
{
    public string Name {get;set;}
}


public EditTeamResponse Edit(TestObject testObject)
    {            
        if (!ValidateTestObject(testObject))
        {
            return testObject;
        }
        try
        {
            _unitOfWork.TestObjectRepository.Update(testObject);
            _unitOfWork.Commit();
        }
        catch (Exception)
        {
            //Error is thrown here
            _validationDictionary.AddError("Unknown", "Unknown Error!");
            return testObject;
        }
        // Other Extra Code
        return editTeamResponse;
    }

protected bool ValidateTestObject(TestObject testObject)
    {
        if (CheckIfNameChanged(teamToValidate))
        {
            if (_unitOfWork.TestObjectRepository.Any(x => x.Name == testObject.Name))
                _validationDictionary.AddError("Name", "Name already exist.");
        }
        return _validationDictionary.IsValid;
    }
private bool CheckIfNameChanged(TestObject testObject)
        {
            return _unitOfWork.TestObjectRepository.FindBy(testObject.TeamId).Name != testObject.Name;
        }

I know that when I call CheckIfNameChanged(TestObject testObject) method i added an Entity Key to the ObjectContext and when i attach or edit the code when i call the _unitOfWork.TestObjectRepository.Update(testObject):

public void Update{
    _context.Entry(entity).State = EntityState.Modified;
}

This is where the conflict happen and i got two same entity key in the ObjectStateManager. Is there a way to solve this problem without me going to the Context to detach the entity or is there some other way? And what is the best way to detach an entity from the context?

  • 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-23T21:12:07+00:00Added an answer on May 23, 2026 at 9:12 pm

    You can check if name changed as follows

    private bool CheckIfNameChanged(TestObject testObject)
    {
        return !_unitOfWork.TestObjectRepository
           .Any(x => x.TeamId == testObject.TeamId && x.Name == testObject.Name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting error An item with the same key has already been added.
I'm getting the following error when calling a stored procedure: Cannot find the object
I'm getting this error: The communication object, System.ServiceModel.ChannelFactory`1[FxCurveService.IFxCurveService], cannot be used for communication because
I'm getting the error Object reference not set to an instance of an object.
I'm getting strange error 'int' object has no attribute 'startswith' I haven't used the
I have been getting an error in VB .Net object reference not set to
I'm getting the NRE error that says: Object reference not set to an instance
in my application I am getting an exception: MyApp(38431,0xa004ffa0) malloc: *** error for object
Getting a rendering error for this form: 'NoneType' object has no attribute 'widget' http://dpaste.com/88585/
I am getting the following error when trying to update an object using nhibernate.

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.