When I execute the following code I don’t get any error, but it doesn’t update the changes on the database either. I have 5 entries in the table user, and after executing the following code there is no user with “Active” state in the database.
Am I supposed to write the update statement myself or does it do it for me? What can be the problem here?
var dbContext = new DataClasses1DataContext();
List<user> users = (from u in dbContext.users
where u.age < 30
select u).ToList();
users[0].state = "Active";
dbContext.SubmitChanges();
EDIT:
So I know what the problem is.
I change the State on my object, and the update statement contains the state as a where clause.
So when executing the query it can’t find an item that matches – it fails on [state] = @p4.
Why is it using all parameters in my update statement when I have a primary key?
UPDATE [dbo].[user]
SET [state] = @p5
WHERE ([id] = @p0) AND ([firstName] = @p1) AND ([lastName] = @p2)
AND ([age] = @p3) AND ([state] = @p4)
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
-- @p1: Input NChar (Size = 10; Prec = 0; Scale = 0) [firstName ]
-- @p2: Input NChar (Size = 10; Prec = 0; Scale = 0) [lastName ]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [25]
-- @p4: Input NChar (Size = 10; Prec = 0; Scale = 0) [Active ]
-- @p5: Input NChar (Size = 10; Prec = 0; Scale = 0) [Active]
You need to change the concurrency pattern on your DBML. This website about Updating LINQ-to-SQL entities explains a bit about it.
Anything you don’t want to include in the
WHEREclause should have theirUpdate Checkset toNeveron the DBML.stateproperty on youruserobjectUpdate Check(the last setting in the properties) toNeverNormally I’d turn off
Update Checkfor all columns but you should do more reading to understand exactly how it works in case you need concurrency.