views:

63

answers:

1

Hey Guys

I'd like to update an entity via linq, but since I edit the entity in a view after serializing it, I don't have direct access to the entity inside the data context. I could do it like this:

entity.property1 = obj.property1;
entity.property2 = obj.property2;
...

thats not cool... not cool at all.

Next thing I tried is to do it via .attach() like so:

context.Table.attach(entity, obj);

doesn't work either. So is there another option short of reflection?

A: 

The Attach() is not working because the entity is still attached to the DataContext. Have you considered creating a new instance of your DataContext for when you reattach your entity? It is generally recommended not to have long-lived DataContexts, instead you should create a new instance per unit of work. This is because as long as you keep a DataContext instance in memory, all of the entities you have retrieved from it remain in memory as well (assuming ObjectTracking is enabled).

From MSDN:

In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx

luksan
I used recursion after all, but your answer is sound :-) thanks
Dänu