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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:05:48+00:00 2026-05-24T11:05:48+00:00

I have a newly entity framework 4 entity that references another entity, a simple

  • 0

I have a newly entity framework 4 entity that references another entity, a simple user record. In my input data, I may have multiple instances of user records that are the same person. That person may not exist in the database; if not, I need to add them by creating a new user entity and adding them.

So when I create the main entity that references the user, I do a quick lookup for that user in the database. If that person exists, great, I got the reference to set. If not, I create a new user record and set the reference in the main entity to the newly created user entity. As I process more records, the same user may pop up, so when I do the query in the “create new user” function, I was assuming that if a newly created but not yet saved entity existed, the query would find that record, but apparently not (ok maybe dumb assumption)… so end result is that a duplicate record is created for that user, linked up to the new main entity, and when it is all saved, I get a “SQL Error 2601: Cannot insert duplicate key row in object ‘dbo.user_table’ with unique index ‘IX_user_table’.” error.

So I’m a bit baffled how to resolve this situation. The problem is that I can’t call SaveChanges() on the context right after adding a new user entity because that causes the entire entity graph to be saved, and the main entity that is referencing the user records is not yet complete, it is not ready to save, so I can’t save the whole mess every time I add a new user. I need to construct all the entities then save it all in one shot.

Do I need to do two queries every time I look up a user, one on the database and one something like:

var unsavedVisitors = from c in
  dataContext.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added)
  where c.Entity is my_table
  select c.Entity;

I really haven’t fiddled with the internals of the entity state manager so I’m not sure what to do here – how can I do a query to find an existing yet unsaved user entity that I could then just link up to the new main entities I’m creating as I loop through my input data? It looks like I can just cast c.Entity above to my_table type and wala, I’ve got my reference. I wish there was just some kind of “include unsaved entities in my query” trick or something, to avoid walking all the unsaved entities with so many queries – is there?

  • 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-24T11:05:48+00:00Added an answer on May 24, 2026 at 11:05 am

    No, there is no “include unsaved entities in my query” trick. It’s the right way to check for entries in the ObjectStateManager. Alternatively you could manage the new added users in some custom dictionary but that’s basically the same like leveraging the ObjectStateManager. Last option would be to clean up somehow your collection of users from duplicate objects in the first place.

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

Sidebar

Related Questions

I have a security rule that a newly registered user has full permissions over
Do I need to save newly created CRM-entity instances before I can set relations
I have a newly defined annotation that is targeted at methods. Is there a
I am new to Entity Framework and hence this question may seem a little
Symptoms I have newly created setup package (MSI) that installs a new version of
I want to use the entity framework. However I also have the requirement of
We have a Silverlight application that uses WCF Data Services. We want to add
Here briefly are the business requirements. I have an entity called PricingSchedule that represents
I have a newly configured Windows Server 2003 VM. One of the issues with
I found a bunch of scripts in the project I have been newly assigned

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.