Update through Linq-to-SQL DataContext not working
I have created a small test for updating table using Linq-to-SQL DataContext as follows:
using (pessimi开发者_Go百科stic_exampleDataContext db = new pessimistic_exampleDataContext())
{
var query = db.test1s.First(t => t.a == 3);
if (query.b == "a")
query.b = "b";
else
query.b = "a";
db.SubmitChanges();
}
But after executing this code in a console application in Main method, when I select records from the table, the record is not updated.
I have debugged through the code, and it is not even throwing any exception.
What can be the problem ?
The issue is resolved. In SQLServer2008, we need to drop and create the table again. I did so and the code worked perfectly.
The Table in Database does not have a primary key, therefore DataContext is not able to update it, I catched this error by inserting another code snippet in the code for inserting a new record into the database using Datacontext and then submitChanges(). In this case it threw an exception telling me that Table does not have a primary key.
精彩评论