I'm using linq to sql to work with database. I have one DataContext instance that load a lot of entities in start of program and fetch them to ram.
Now I need to create a new DataContext and load entities from my first DataContext instead of getting them from database. (because of speed problem in getting that entities from database in local network)
something like this:
DataContext _globalDC = DB._dc;
//Do some entity loading ...
DataContext _localDC = _globalDC.Clone()
Now I should be able to act normal with _localDC (search, insert, update, ...) without effecting _globalDC.
Is it possible?
Not possible. DataContext
doesn't have a native Clone
method, so if you'd create an extension method namded Clone
, you could only copy the public properties. But it's the context's internal state that makes it a working context (meta data and state tracking and all).
Your best chance is to clone the entity objects and attach them to a new context.
But I think what you're actually looking for is a caching solution that works with LINQ-to-SQL. This may be a good starting point: How do you implement caching in Linq to SQL?