I do my query...
var result = from u in tdc.tblUsers
where u.UserID == userID
select u;
and then I change the values I want to:
foreach (tblUsers u in result)
{
//change values (and no im not changing the primary key or foreign keys)
}
then I submit changes
tdc.SubmitChanges();
When it hits submit changes, it throws exception that the row wasn't found or was changed. I am the only person using this so there's no other conflicts with accessing the db or locking. Why would it throw the ChangeConflictException
? I have stepped through with debugger and the data persists all the way through the process, including the changes that I'm trying to make.
I have also tried it this way before and got the same error
tblUsers result = (from u in tdc.tblUsers
where u.UserID == userID
select u).Single();
result.Address = address;
result.Phone = phone;
tdc.SubmitChanges();
It will only ever retrieve 1 record with this query since UserID
is the primary key.
I've done this many times and it has worked. Every example I've found is exactly what I have.
The UPDATE command in SQL is used to modify or change the existing records in a table. If we want to update a particular value, we use the WHERE clause along with the UPDATE clause. If you do not use the WHERE clause, all the rows will be affected.
LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.
UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition; table_name: name of the table column1: name of first , second, third column.... value1: new value for first, second, third column.... condition: condition to select the rows for which the values of columns needs to be updated.
When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language.
Maybe you work with different context? Try to encapsulate it by using
using (myContext ctx = new myContext())
{
var user = ctx.users.first();
user.name="blah";
ctx.SubmitChanges();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With