Can I use the auto-generated Linq-to-SQL entity classes in 'disconnected' mode?
Posted
by Gary McGill
on Stack Overflow
See other posts from Stack Overflow
or by Gary McGill
Published on 2010-03-15T09:55:14Z
Indexed on
2010/03/15
9:59 UTC
Read the original article
Hit count: 333
Suppose I have an automatically-generated Employee
class based on the Employees
table in my database.
Now suppose that I want to pass employee data to a ShowAges
method that will print out name & age for a list of employees. I'll retrieve the data for a given set of employees via a linq query, which will return me a set of Employee
instances. I can then pass the Employee
instances to the ShowAges
method, which can access the Name
& Age
fields to get the data it needs.
However, because my Employees
table has relationships with various other tables in my database, my Employee
class also has a Department
field, a Manager
field, etc. that provide access to related records in those other tables. If the ShowAges
method were to invoke any of those methods, this would cause lots more data to be fetched from the database, on-demand.
I want to be sure that the ShowAges
method only uses the data I have already fetched for it, but I really don't want to have to go to the trouble of defining a new class which replicates the Employee
class but has fewer methods. (In my real-world scenario, the class would have to be considerably more complex than the Employee
class described here; it would have several 'joined' classes that do need to be populated, and others that don't).
Is there a way to 'switch off' or 'disconnect' the Employees
instances so that an attempt to access any property or related object that's not already populated will raise an exception?
If not, then I assume that since this must be a common requirement, there might be an already-established pattern for doing this sort of thing?
© Stack Overflow or respective owner