在我的数据上下文 CalcinerDataContext 中,我有一个类 MainData 和一个简单继承的类 NewSample(仅用于探索此错误)。下面的代码运行没有任何错误:
Dim cdc As New CalcinerDataAccess.CalcinerDataContext
Dim newData As New CalcinerDataAccess.MainData
With newData
.LotNumber = "Test1"
.ProcessType = 4
.SubmitterID = 2
.Calciner = 1
.DateEntered = Now
End With
Try
cdc.MainDatas.InsertOnSubmit(newData)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
MsgBox(ex.Message)
End Try
下面的代码生成以下错误: System.NullReferenceException:对象引用未设置为对象的实例。在 System.Data.Linq.Table`1.InsertOnSubmit(TEntity 实体)。请注意,这两个代码块之间的唯一区别是 newData 的实例化
Dim cdc As New CalcinerDataAccess.CalcinerDataContext
Dim newData As New CalcinerDataAccess.NewSample
With newData
.LotNumber = "Test1"
.ProcessType = 4
.SubmitterID = 2
.Calciner = 1
.DateEntered = Now
End With
Try
cdc.MainDatas.InsertOnSubmit(newData)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
MsgBox(ex.Message)
End Try
为什么继承的类会抛出异常,而基类不会呢?