简体   繁体   中英

LINQ to SQL:DataContext.SubmitChanges not updating immediately

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?

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);
}

Change the c.DataCompliances.Where... to DC.DataCompliances.Where...

DC is the data context, what is the definition of c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM