派生类导致 InsertOnSubmit() 抛出 NullReferenceException
我确实查看了已回答的相关帖子,但仍然在 InsertOnSubmit()
中遇到 NullReference
异常。
我有一个自动生成的 LINQ 类事务,我从中创建了一个重写 ToString() 的派生类:
public partial class MyTransaction:DataAccess.Transaction
{
public MyTransaction():base()
{
}
public override string ToString()
{
return "some text";
}
}
插入如下:
public bool InsertTransaction(Transaction t)
{
using (MarketPricesDataContext dataContext = new MarketPricesDataContext(connectionString))
{
try
{
dataContext.Transaction.InsertOnSubmit(t);
dataContext.SubmitChanges();
return true;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return false;
}
}
}
我想从 < 调用基本构造函数code>MyTransaction 可以解决我的问题,但事实并非如此。我可以插入事务项,但当我尝试插入 MyTransaction
项时,我收到 NullReferenceException
。
这有效:
Transaction t=new Transaction();
t.Type="A"; //set some values
Data d=new Data();
d.InsertTransaction(t);
这会在 InsertOnSubmit()
中抛出 NullReferenceException
(对象引用未设置为对象的实例。):
MyTransaction myt=new MyTransaction();
myt.Type="A"; //set some values
Data d=new Data();
d.InsertTransaction(myt as Transaction);
我做错了什么?
I did look at the related posts that were answered, but still get the NullReference
exception in InsertOnSubmit()
.
I have an auto generated LINQ
class Transaction from which I create a derived class that overrides ToString()
:
public partial class MyTransaction:DataAccess.Transaction
{
public MyTransaction():base()
{
}
public override string ToString()
{
return "some text";
}
}
And the insert is as follows:
public bool InsertTransaction(Transaction t)
{
using (MarketPricesDataContext dataContext = new MarketPricesDataContext(connectionString))
{
try
{
dataContext.Transaction.InsertOnSubmit(t);
dataContext.SubmitChanges();
return true;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return false;
}
}
}
I thought calling the base constructor from MyTransaction
would solve my problem, but it did not. I can insert Transaction items but when I try to insert a MyTransaction
item I get a NullReferenceException
.
This works:
Transaction t=new Transaction();
t.Type="A"; //set some values
Data d=new Data();
d.InsertTransaction(t);
This throws a NullReferenceException
(Object reference not set to an instance of an object.) in InsertOnSubmit()
:
MyTransaction myt=new MyTransaction();
myt.Type="A"; //set some values
Data d=new Data();
d.InsertTransaction(myt as Transaction);
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LINQ-to-SQL 已支持继承,但仅限于模型中的类型。我想它正在模型中寻找您的自定义类型(以找到适当的表或鉴别器),但失败了。
简而言之,我认为您不能使用不模型一部分的子类型。
然而,这些类型是
部分
的;您应该能够执行以下操作:将您的
ToString()
覆盖添加到生成类型中。LINQ-to-SQL already supports inheritance, but only for types in the model. I imagine it is looking for your custom type in the model (to find the appropriate table or discriminator), and failing.
In short, I don't think you can use subtypes that aren't part of the model.
However, the types are
partial
; you should be able to do:which adds your
ToString()
override into the generated type.感谢马克为我指明了正确的方向。为了解决这个问题,我取消了
MyTransaction
类和DataAccess
命名空间,其中包含DataContext
和 Linq-to-SQL类中,我重写了ToString()
,如下所示:我现在使用
Transaction
,而之前我会使用MyTransaction
。Thanks Marc for pointing me in the right direction. To solve the problem, I did away with the
MyTransaction
class and in theDataAccess
namespace, the one containing theDataContext
and Linq-to-SQL classes, I overrideToString()
as follows:I now use
Transaction
where I before would have usedMyTransaction
.