In my application I have several DataContexts that connects to different databases with different schemas. In a custom user control I display the results of the query and let the user edit them, and when the user edits the data I want to persist the changes to the database. To do that I need a reference to the source DataContext (or at least the source datacontext type) so I can do a DataContext.SubmitChanges();
Is there any way to determine which DataContext a query comes from? The DataQuery class itself is marked as internal, so I can’t access its context property without resorting to ugly reflection hacks, so I’m looking for a cleaner approach.
There are (several) ways around this problem, passing along a reference to the source DataContext for instance, but I imagine there must be a simpler way to do this.
Edit: The following code works, but it’s ugly:
FieldInfo contextField = query.GetType().GetField('context', BindingFlags.Instance | BindingFlags.NonPublic); if (query != null) { queryContext = contextField.GetValue(value) as DataContext; }
I think you’re going to have to simply pass the DataContext to the code (manually). Sorry.