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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:10:13+00:00 2026-06-11T19:10:13+00:00

I am trying to update an entity with a FK relationship in EntityFramework 4.3

  • 0

I am trying to update an entity with a FK relationship in EntityFramework 4.3 Code First.
I try to attach to the related entites by calling: Entry(item).State = EntityState.Unchanged

I get the following exception: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

I do not update these items nor have an id property for them on my main entity.
Is it possible to know which entities are attached or not ?

Thanks in advance,
Radu

  • 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-06-11T19:10:14+00:00Added an answer on June 11, 2026 at 7:10 pm

    You can find the answer here.

    public bool Exists<T>(T entity) where T : class
    {
        return this.Set<T>().Local.Any(e => e == entity);
    }
    

    Place that code into your context or you can turn it into an extension like so.

    public static bool Exists<TContext, TEntity>(this TContext context, TEntity entity)
        where TContext : DbContext
        where TEntity : class
    {
        return context.Set<TEntity>().Local.Any(e => e == entity);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to update my entity using Entity Framework 4.1 code First approach
I'm trying to update an entity and its related entities as well. For instance,
Im trying to update a table with the following code. If I change WHERE
Im trying to update the contents of an element after running some php code.
I'm trying to update my code to use cv2.SURF() as opposed to cv2.FeatureDetector_create(SURF) and
Is there a way in Entity Framework 4 (using CTP4 and Code First if
I am trying to update the entity that has some columns with foreign keys.
We're trying to update an entity via WCF, and the entity contains a Timestamp
I am trying to update an entity using Entity Framework 4.1 in my asp.net
I've inherited a project which uses Entity Framework Database First. I'm trying to work

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.