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