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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:12:57+00:00 2026-05-19T03:12:57+00:00

I have a funny problem. Doing DataContext.SubmitChanges() updates Count() in one way but not

  • 0

I have a funny problem.

Doing DataContext.SubmitChanges() updates Count() in one way but not in the other, see my comment in the code below.
(DC is the DataContext)

  Company c = DC.Companies.SingleOrDefault(x => x.Name == companyName);
  DataCompliance compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

  if (compliances.Count() == 0) // Insert if not exists
  {
    DC.DataCompliances.InsertOnSubmit(new DataCompliance {
      FKCompany = c.Id,
      FKComplianceCriteria = criteria.Id
    });
    DC.SubmitChanges();

    compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);

    // At this point DC.DataCompliances.Count() has increased,
    // but compliances.Count() is still 0
    // When I refresh the page however, it will be 1
  }

Why does that happen?

I need to update compliances immediately after inserting one. Does anyone have a solution?

  • 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-19T03:12:57+00:00Added an answer on May 19, 2026 at 3:12 am

    There’s a difference between adding the object to the data context (and propagating it back to the database) and adding it to the associated entity collection of an object you’ve retrieved from the database. Once you’ve retrieved an object and its associated entities, making changes to the data in the database won’t be reflected in the retrieved object because the database isn’t required. The key bit is the SingleOrDefault() on the company — that forces the query to be executed and the retrieved data assigned. Enumerating the associated entities will cause them to be loaded if they haven’t been eagerly loaded already. Thus, even after updating the database, the previously retrieved object won’t reflect the update. You can, however, add the inserted object to the company’s data compliances collection, then do the update and it will be what you expect. Note that I think you still need to assign the associated entity which you’re using in your check. Refreshing the page also solves the problem because the data is requeried from the database, updating the associated entity collections as well as the tables on the data context.

    Company c = DC.Companies.SingleOrDefault(x => x.Name == companyName);
    var compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
    
    if (compliances.Count() == 0) // Insert one if not exist yet
    {
        c.DataCompliances.Add( new DataCompliance
        {
            ComplianceCriteria = criteria
        });
        DC.SubmitChanges();
    
        compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
    }
    

    Original (left for context)

    What if you try assigning the associated entity instead of just its id? I’m believe that assigning the id doesn’t actually populate the associated entity that’s being checked and SubmitChanges is only propagating the data back to the DB, not actually updating the table element with the data for the associated entity.

    var compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
    
    if (compliances.Count() == 0) // Insert one if not exist yet
    {
        DC.DataCompliances.InsertOnSubmit(new DataCompliance {
            FKCompany = c.Id,
            ComplianceCriteria = criteria
        });
        DC.SubmitChanges();
    
        compliances = c.DataCompliances.Where(x => x.ComplianceCriteria.FKElement == e.Id);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bizarre problem that is doing my head in. I have the
Quite a funny question I have. I am working now on the HTML parser
Hi I have a difficult regex problem that I have tried and have a
I'm struggling with a (usually simple to deal with) problem. I have a database
I have created a RIA services class library project and things are not going
I have content management system application that uses a polymorphic tree table as the
I'm having a problem writing Norwegian characters into an XML file using C#. I
I'm a jstl newbie so probably this question will sound to you funny. Anyway,
My Goal I would like to have a main processing thread (non GUI), and
i am haveing some problems with JSON and arrays. I have been messing around

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.