开发者

c# DataContext.SubmitChanges() not updating database

开发者 https://www.devze.com 2023-02-18 06:59 出处:网络
Can someone explain why the following code fails to update the database or what else I can do to troubleshoot?

Can someone explain why the following code fails to update the database or what else I can do to troubleshoot?

// *********************************
// People Updates
// *********************************
// In Engr and SoE
EmplIDs = InputList.GetPeopleIds(InputType.Engr | InputType.SoE);       // retrieve IDs in tables Engr and SoE
Engr = DB.GetEngrByIds(EmplIDs);                                        // retrieve objects from Engr
SoE = DB.GetSoEByIds(EmplIDs);                                          // retrieve objects from SoE
Batch.Update开发者_如何学运维(SoE, Engr);                                                // update SoE with Engr data
DB.Save(SoE);                                                           // persist


// Inside DB repository
public void Save(List<SoE_People> people) {
    ChangeSet cs = dc.GetChangeSet();
    foreach (SoE_People person in people.Where(p => cs.Updates.Contains(p))) {
        person.LastUpdate = DateTime.Now;
    }

    dc.SubmitChanges();
}

I've checked the following:

  • people.Count ~ 2500, cs.Updates.Count ~ 200
  • dc.GetChangeSet.Updates.Count = 0 after calling SubmitChanges()
  • all updates to the people object are correct. They are visible in the locals window via people and cs.Updates[x]
  • no exceptions are thrown by dc.SubmitChanges()
  • setting dc.Log = Console.Out shows no SQL for the SubmitChanges()
  • a previous section of the code that inserts new records via dc.InsertAllOnSubmit() works fine -> no write permission problem.
  • manually cutting and pasting data into the SoE_People table works -> no foreign key constraint problem.


Without even looking into the logic you have above, here are some recommendations:

  • Put everything in a try/catch. How do you know there's no exception being thrown?
  • dc.SaveChanges() returns an int - number of records that were CRUDed. Capture the return value and check it.


I'm not familiar with DataContext (don't use it), but I'll throw this out there.

If it's like an SqlDataAdapter, did you write/define the SQL text for inserting or updating your records?


You have probably overridden SoE_People update in the datacontext. There should be a this .ExecuteDynamicUpdate(instance).

Look there maybe you have commented / removed that.

You can take a look here if you don't understand what I mean: Custom Entity Insert/Update/Delete Method Validation.


Maybe it is not the code, it is the database table. I had the issue to update the table use that SubmitChanges(). I fixed the problem by giving the table a primary key.

0

精彩评论

暂无评论...
验证码 换一张
取 消