1

If I'm extending OnCreated for a LINQ to SQL table object, is it possible to get a reference to the data context to which the table belongs? For example, if I add a property to the data context:

Partial Class MyDataContext

    Private _myValue As String
    Public ReadOnly Property MyValue As String
       Get
           Return _myValue
       End Get
       Set(ByVal value As String)
           _myValue = value
       End Set
    End Property

End Class

is there any way to access that value in the create event of the table, i.e.:

Partial Class MyTable

    Private Sub OnCreated()
        Dim contextValue = [data_context_reference_here].MyValue
    End Sub

End Class

I don't want the property on the data context to be Shared, because it could be different per instance. I've been pouring over the designer code to figure out where the reference might be, but no luck yet. Any ideas?

2
  • 1
    What reason makes you think you need to access that property? Perhaps it isn't necessary to do it this way... more info please Commented Jul 27, 2009 at 16:33
  • It's for debugging. I'd like to pass one instance of the debugging class to the context, then be able to access it from any event on any table in that context. (So "MyValue" would be the debugger instance - I just made it a string for simplicity.) Commented Jul 27, 2009 at 17:38

3 Answers 3

1

There are no generated table objects in LINQ to SQL--only row objects, of which the DataContext owns one Table(Of TRowType) per. So the partial OnCreated method you implement is in a row class, and will be called whenever a row is created.

Generated row objects implement INotifyPropertyChanged and INotifyPropertyChanging, but are not derived from any base class. Since OnCreated takes no parameters, there is no way for the row to determine (via that method) which Table it belongs to, much less which DataContext it was created for.

You'll have to find some other way to do what you want.

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

Comments

1

I will say it is an incorrect pattern for Linq 2 SQL

In Linq2sql, it is supposed that any row object like your MyTable can be created without context. Later you can attach it to a table (with a specified context)

For example

Dim myTable as new MyTable()
dataContext.GetTable(Of MyTable).Attach(myTable)

So in reality, you should not create any logic inside MyTable classes that will depend on datacontext that was used to create this object, because some of them can be created without dataContexts, some can be attached and detached...

Comments

0

A simple google search (linq event data context) would have led you to where this question was already asked and answered:

Determine the source DataContext for a Linq to Sql query

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.