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 5844673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:19:35+00:00 2026-05-22T12:19:35+00:00

when i try to attach entity to context i get an exception An object

  • 0

when i try to attach entity to context i get an exception

An object with the same key already
exists in the ObjectStateManager. The
ObjectStateManager cannot track
multiple objects with the same key

This is expected behaviour.

But i would like to know how ObjectStateManager knows that?
I would like to do this check by myself before

  • 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-22T12:19:35+00:00Added an answer on May 22, 2026 at 12:19 pm

    If you are using DbContext API (you mentioned ef-code-first) you can simply use:

    context.YourEntities.Local.Any(e => e.Id == id);
    

    or more complex

    context.ChangeTracker.Entries<YourEntity>().Any(e => e.Entity.Id == id);
    

    In case of ObjectContext API you can use:

    context.ObjectStateManager.GetObjectStateEntries(~EntityState.Detached)
                              .Where(e => !e.IsRelationship)
                              .Select(e => e.Entity)
                              .OfType<YourEntity>()
                              .Any(x => x.Id == id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
I try Request.Form.Set(k, v) but it's throwing exception Collection is read-only
Using ASP.NET MVC and Entity Framework, I encounter some attach/detach errors when I need
In my database application, when I try to attach a page to a site,
Is there a way to test if a collection is already initialized? try-catch only?
I try to do the following with entity framework 4 : public void Update(Site
When I try to attach a .mdf file in Management Studio to open it
I try to attach action Events on the JCombobox arrow JButton. So I make
I detach a database file from a SQL Server 2000 and try to attach
I get the object, using NoTracking, convert and manipulate it, send it to an

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.