Updating linq to sql entities

But the experience is still not what I would call intuitive.

Ok, so the following code is an aproximation of a very simple disconnected operation.

Now to get this to work I can do the following: [Region] = @p7, [Postal Code] = @p8, [Country] = @p9, [Phone] = @p10, [Fax] = @p11 WHERE ([Customer ID] = @p0) AND ([timestamp] = @p1) SELECT [t1].[timestamp] FROM [dbo].[Customers] AS [t1] WHERE ((@@ROWCOUNT) '@p0 nchar(5),@p1 varbinary(8),@p2 nvarchar(19),@p3 nvarchar(12),@p4 nvarchar(20),@p5 nvarchar(22), @p6 nvarchar(6),@p7 nvarchar(4000),@p8 nvarchar(5),@p9 nvarchar(7),@p10 nvarchar(11),@p11 nvarchar(11), @p12 nchar(5)' So at this point you can see LINQ is updating every field which is to be expected given that it has no change information.

updating linq to sql entities-89

So the concurrency check is already occurring anway so no worries there. If there are differences they would be detected by the WHERE clause in the first place.

This whole update mechanism and Attach seems completely redundant in light of the SQL already going to the server.

Do not try to Attach an entity that has not been detached through serialization.

Entities that have not been serialized still maintain associations with deferred loaders that can cause unexpected results if the entity becomes tracked by a second data context.

Another oddity here: If you use the Attach(cust,cust2) if the table has a Time Stamp field, Submit Changes also fails with: I suspect that's a bug in the way Attach updates the entity from the 'original state' and is inadvertantly updating the Time Stamp field.

This happens inside of the LINQ code - the Update never hits the SQL backend. It works - but if you ask me the process is about as clear as mud.

If you pass just the entity the entity is attached only but the changes that might exist are invisible - only explicit changes you make after Attach will update. Now this makes some sense - you can basically tell LINQ to attach and then compare the object state against an existing instance and based on that update the change state.

Ok, that works, but it requires a Time Stamp field for every table. So what I unsuccessfully tried in my previous post is code like the following: I load up a second instance from the context and use that as a comparison.

Use the Attach methods with entities that have been created in one Data Context, serialized to a client, and then deserialized back (with the intention to perform an update or delete operation).

For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

So how are you supposed to get a current instance to compare against?

Tags: , ,