views:

28

answers:

3

Hi,

I have a .NET 4 WinForms app that uses the ADO.NET Entity Framework. Some code that was working perfectly has decided to stop working and although I've tried to figure out what changed that could cause this, I am stumped. This code looks perfectly functional to me and WAS working as intended.

Anyone have any ideas? Here is the code:

using (var pe = new ProposalEstimateEntities()) 
{
    var tmc = GetToolingAndMaterialsCost(taskId, Constants.Materials);

    if (tmc == null) return;

    tmc.Amount = amount;
    pe.SaveChanges();
}

The tmc variable holds a ToolingAndMaterialsCost entity object after the method call, so there is no need to add the object to the context. SaveChanges() should save the object with the new amount, but doesn't. No exception is thrown. The code executes like it is dumb and happy, but nothing changes in the database and when I return to the control that displays the data, the old pre-edit data is there.

This is very frustrating, any help would be greatly appreciated.

Mike

A: 

I'd step through w/the debugger to make sure tmc isn't null. If tmc is indeed an entity object with an Amount field, that should work just fine and the problem is presumably elsewhere. How bout the code from the GetToolingAndMaterialsCost method?

Phil Winkel
A: 

How was tmc loaded without any reference to the ObjectContext 'pe'? Looks like you may be loading it from a different context??

If so, that won't work. The context tracks entities that have changed. You 'pe' context isn't tracking any entities by the looks of it since it's never referenced except in the SaveChanges call.

Hightechrider
Correct, the context used to load the entity is different than the one used in the SaveChanges call. I added the tmc object to that context and it worked.
flyfisher1952
A: 

Doh!

The call to GetToolingAndMaterialsCost uses a different ProposalEstimateEntities object than the one in the method, so I just added the entity object to the data context and it worked fine. OUtsmarted myself when I refactored.

flyfisher1952