Adding Linked Data with Multiple LINQ to SQL DataContexts

I am developing a desktop application using Linq to SQL. From what I have gathered it is better to keep DataContext objects short-lived, but I ran into the following puzzle:

I have a lot of search type data in my application that I would like to cache on the client. The reason I want to cache it is because I don't want to look for the corresponding parent record for every child record that I need to insert when I do large ETL operations.

When I perform database updates, I need to associate my updated records with this search data. Now, with one global DataContext, it's simple: I just add new children to the collection of search object relationships and hit SubmitChanges ().

But if I create new DataContexts from the ones I originally fetched the data from, what is the best way to manage the updating of this related data? I cannot use the cached data with my new DataContext because it was coming from another.

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

Thanks a lot in advance.

+1


source to share


2 answers


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

More details here .



Another thing you could do is look at nHibernate and use Linq for nHibernate .

+1


source


Entity Framework tends to struggle with you even harder when it comes to data outages. You can use LINQ-to-SQL with app / detach easily. Of course, you don't always need to reference the associated data; you also have the option to set the appropriate id - i.e. you usually have a couple of properties:

  • SomethingId (foreign key id value)
  • Something (link)


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

+1


source







All Articles