views:

465

answers:

2

Hi,

I am using this simple code to try and update a row

    var MyDB = new SKN2DataContext();

    var s_case = MyDB.SupportCases.FirstOrDefault(sc => sc.Id == 3);

    s_case.OpenDate = DateTime.Now;

    MyDB.SubmitChanges();

On the last line i am presented with the following exception

Value of member 'Id' of an object of type 'SupportCase' changed. A member defining the identity of the object cannot be changed. Consider adding a new object with new identity and deleting the existing one instead.

When I see what's going on by calling GetChangeList in QuickWatch, it says that the DataContext is trying to do and update (which i expect) as well as an insert of the same row (WHY???). I can't figure out why the DataContext is trying to do an insert. Please help!!!

EDIT Just for the record OpenDate is not included in the primary key

A: 

Is the OpenDate part of the primary key? I would look there first.

Is s_case not null?

Daniel A. White
I don't understand what u mean by s_case being 'not null'. Its just an object returned by my lambda expression. How is it supposed to be null or not null?
Ali Kazmi
Well with `FirstOrDefault` I've seen it return `null`. Are you checking for this case?
Daniel A. White
Actually the exception is raised on the last line. Had it been the case u are suggesting, the exception would be raised on the second last line. And i know for a fact that it's not null
Ali Kazmi
Hm. then im stumped.
Daniel A. White
A: 
  • Isolate the code you are using, to make sure nothing else is causing it (like a shared context)
  • Re-add the table in the linq2sql designer.
eglasius