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

The Archive Base Latest Questions

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

Last week I had a bug that I tried to recreate. A piece of

  • 0

Last week I had a bug that I tried to recreate. A piece of existing code did a mass insert on the database. Though, a value was not allowed to be duplicate in the database The old code persisted the changes to the database after every AddObject with a SaveChanges. This was not performing very well. So, instead of saving it every time I saved it after every 1000 records (and did this in a transaction,but that is not relevant for this sample).

this code below gives a overview of what I did.

        TestEntities test = new TestEntities();

        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                string q = j.ToString();
                if (!test.warehouses.Any(x => x.combi == q))
                {
                    warehouse wh = new warehouse();
                    wh.combi = j.ToString();
                    wh.ean = j.ToString();
                    test.warehouses.AddObject(wh);
                }
            }

        }
        test.SaveChanges();

What I did not know was that entity framework only queries the data in the database and not the pending data so the any query gives no result on the database (and I was assuming that there would be a result.) Hence, this results in unwanted duplicates in the code above.

For now I solved it to store all soon to be added objects in memory and then store them in the database. This works, but gives a lot of overhead in the code. Is there a way to work around this issue? Can you tell EF to work on the database and the pending changes? Are all ORM’s working like this?

Thanks,
Patrick

  • 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-11T11:13:08+00:00Added an answer on June 11, 2026 at 11:13 am

    Faced with similar problem I decided to check database as you do and check local data:

        if( !test.warehouses.Local.Any(x => x.combi == q ) 
            && !test.warehouses.Any(x => x.combi == q ) )
    

    This should work.

    Edit:
    But it doesn’t. However, this does:

    var isLocal = ( test as System.Data.Objects.ObjectContext ).ObjectStateManager
        .GetObjectStateEntries( System.Data.EntityState.Added )
        .Any( x => (x.Entity as warehouse).combi == q );
    if( !isLocal && !test.warehouses.Any( x => x.combi == q ) ) {
        // ...
    }
    

    I have to note that IDbSet<> (used by my code first instead of ObjectSet<> created by model designer) has .Local property so there’s no need to query ObjectStateManager. I know that code first is completely different thing but you can be very efficient with it, too: design database with Microsoft SQL Server Management Studio and run excellent Reverse Engineer Code First from Entity Framework Power Tools with default or customized (if needed) T4 templates to get almost anything. http://en.wiktionary.org/wiki/your_mileage_may_vary

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

Sidebar

Related Questions

I observed something last week that I did not expect, and will describe below.
So I had this working last week. At least, I thought I did! DataGridView
I've spent the last week developing code to connect to a Web Service using
In the last week I generated a SSH id_rsa key on my localhost that
I had this same problem last week, which resolved itself after I enabled impersonation.
A friend and I had an argument last week. He stated there were no
Last week I had an interview. I was asked to write a macro to
Last week I handled some object casting (DataGridView Columns Control casts) and I tried
Recently I had to fix a bug that was reported from the field. While
I had been using Xcode 4.2 up till last week and I was using

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.