1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.