views:

517

answers:

2

Hi:

I'm developing a desktop application using Linq to SQL. From what I've gathered, it's better to keep DataContext objects short-lived, but I'm running into the following puzzle:

My application has lots of lookup-type data that I'd like to cache on the client. The reason I want to cache it is because I don't want to have to look up the appropriate parent record for every child record I need to insert when I'm doing large ETL-type operations.

When I perform updates to the database, I need to relate my updated records to this lookup data. Now, with a single, global DataContext, that's easy: I just add the new child records to the relation collections of the lookup objects and hit SubmitChanges().

But if I'm creating new DataContexts from the ones that I originally fetched the data from, how can I best manage updating this related data? I can't use the cached data with my new DataContext because it came from a different one.

This is all pretty confusing - should I give up and learn the Entity Framework instead?

Thanks much in advance.

+1  A: 

You can use detach/attach to remove the association to the data context with which you retrieved the object, then connect it to a new data context when you are ready to update it.

More info here.

The other thing that you could do is look at nHibernate and use Linq for nHibernate.

tvanfosson
+1  A: 

Entity Framework tends to fight you even harder when it comes to disconnecting data. You should be able to use LINQ-to-SQL with attach/detach fairly easily. Of course, you don't always need to set a reference to associate data; you also have the option of setting the related id - i.e. you commonly have a pair of properties:

  • SomethingId (the foreign key identity value)
  • Something (the reference)

You might be able to just set the Id without touching the reference.

Marc Gravell