如何设置 DataContext.CommandTimeout 应用程序范围

How to set the DataContext.CommandTimeout application wide

我想为我的 DataContext Class 应用程序设置超时。我可以找到很多 C# 示例 (1, 2) 翻译成 vb.net 它将是

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

但是报错: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.

我现在有两个问题: 我只是无法解决 "sub new" 问题。如果我放一个

Sub New()
End Sub

我收到此构造函数未退出的错误,但我不确定应该使用哪个现有构造函数,因为我之前从未使用过 Partial Class

第二个是我不确定我必须使用什么 SampleDBDataContext,因为我有一个名为 MySolution 的解决方案,其中有一个 MySolution.SampleDBDataContext,还有 System.Date.Linq.DataContext

解决方案是您需要为从 .dbml 生成的 DataContext class 创建部分 class。新建的partial需要和自动生成的partial有对应的namespace和name,这样才能被识别为class.

的一个单元

一旦您的 class 被识别为自动生成的 DataContext class 的一部分,您甚至不需要手动继承 System.Data.Linq.DataContext,因为自动生成的一个已经继承 System.Data.Linq.DataContext.

关于错误信息:

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.

那是因为 SampleDBDataContext 继承了 System.Data.Linq.DataContext 而没有提供构造函数 (Sub New)。基础 class 没有无参数构造函数——这是在 class 初始化时调用的默认构造函数——因此编译器无法推断它应该调用哪个参数化构造函数以及参数应该如何调用供应。在这种情况下的解决方案不是在您的部分 class 中提供构造函数,而是让它对应于部分 class 的正确 'other side',即自动生成的。自动生成的 DataContext 已声明所有需要的构造函数,因此您的部分 class 没有义务提供一个。