Question

I am using Linq2Sql to update a row data but once I change the values I've researched this issue before and found the following possible reasons:

  • Entity was not changed so no update occurs
  • Entity is missing a primary key but no update occurs

None of these are the case in my situation.

I have my PK in my class and table. The GetChangeSet() of the data context indicates that there is atleast 1 update.

The only issues I see is that from the data context log is that no update statement is generated.

Does anyone have an idea what the issue could be.

Here is a sample of the code:

using(context db=new context())
{
 db.Log=new System.IO.StreamWriter(sample){AutFlush=true};
 MyObject obj=db.MyTable.SingleOrDefault(row=>Email==email);
 if(obj!=null)
 {
   obj.FirstName=firstName;
   obj.LastName=lastName;
   System.Data.Linq.ChangeSet set=db.GetChangeSet();
   db.SubmitChanges();
 }
}
Was it helpful?

Solution 2

I found the problem. For anyone interested.

I previoulsy added validation logic to my DataContext Clas. For example if my Table name is "Product" I added validation to the partial method "UpdateProduct".

However I neglected to tell LINQ to SQL to continue with changes to the database. So it would validate then do nothing becuase I did not continue the code.

For anyone who needs it, in order to do so properly I needed to call "ExecuteDynamicUpdate" when the logic passed.

Sample of the updated solution:

partial void UpdateObj(Obj instance)
{
  if(instance.Field==null)
  {
     thow new NotImplementedException();
  }
  //Added missing code continuation
  this.ExecuteDynamicUpdate(instance);
}

The ScottGu Blog helped me alot...http://weblogs.asp.net/scottgu/linq-to-sql-part-4-updating-our-database

OTHER TIPS

You can try this

if(obj!=null)
 {
   obj.FirstName=firstName;
   obj.LastName=lastName;
   db.MyTable.InsertOnSubmit(obj);
   db.SubmitChanges();

   System.Data.Linq.ChangeSet set = db.GetChangeSet();
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top