Search code examples
vb.netlinqdatacontextcommand-timeout

How to set the DataContext.CommandTimeout application wide


I want to set the timeout for my DataContext Class application wide. I can find a lot of examples for C# (1, 2) translated to vb.net it would be

Partial Class SampleDBDataContext
    Inherits System.Data.Linq.DataContext
    Partial Private Sub OnCreated()
        'Put your desired timeout here.
        Me.CommandTimeout = 3600
    End Sub
End Class

But it gives the error: Class '<classname1>' must declare a 'Sub New' because its base class '<classname2>' has more than one accessible 'Sub New' that can be called with no arguments.

I have two questions now: I am just not able to solve the "sub new" problem. If I put a

Sub New()
End Sub

I get the error that this constructor doesnt exit but I am not sure what existing constructor I should use since I never used a Partial Class before.

The second is that I am not sure what SampleDBDataContext I have to use since I have a solution called MySolution where I have a MySolution.SampleDBDataContext and I also have System.Date.Linq.DataContext.


Solution

  • The solution is that you need to create partial class for DataContext class generated from .dbml. The newly created partial need to have corresponding namespace and name as the auto-generated one so that they can be recognized as one unit of class.

    Once your class recognized as partial of the auto-generated DataContext class, you don't even need to manually inherits System.Data.Linq.DataContext, because the auto-generated one already inherits System.Data.Linq.DataContext.

    Regarding the error message :

    Class '<classname1>' must declare a 'Sub New' because its base class '<classname2>' has more than one accessible 'Sub New' that can be called with no arguments.

    that's because SampleDBDataContext inherits System.Data.Linq.DataContext without providing constructor (Sub New). The base class doesn't have parameterless constructor -which is the default constructor to call upon intialization of the class- so that compiler can't infer which parameterized constructor should it call and how the parameters should be supplied. The solution in this case isn't to provide constructor in your partial class, but to have it corresponds to the correct 'other side' of partial class, that is the autogenerated one. Auto-generated DataContext has all constructors needed declared so that your partial class doesn't have obligation to provide one.