派生类导致 InsertOnSubmit() 抛出 NullReferenceException

发布于 2024-09-18 13:16:28 字数 1495 浏览 1 评论 0原文

我确实查看了已回答的相关帖子,但仍然在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酸甜透明夹心 2024-09-25 13:16:28

LINQ-to-SQL 已支持继承,但仅限于模型中的类型。我想它正在模型中寻找您的自定义类型(以找到适当的表或鉴别器),但失败了。

简而言之,我认为您不能使用模型一部分的子类型。

然而,这些类型是部分的;您应该能够执行以下操作:

namespace MyTransaction:DataAccess {
    partial class Transaction {
        public override string ToString() {
            return "some text";
        }
    }  
}

将您的 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:

namespace MyTransaction:DataAccess {
    partial class Transaction {
        public override string ToString() {
            return "some text";
        }
    }  
}

which adds your ToString() override into the generated type.

遇见了你 2024-09-25 13:16:28

感谢马克为我指明了正确的方向。为了解决这个问题,我取消了 MyTransaction 类和 DataAccess 命名空间,其中包含 DataContext 和 Linq-to-SQL类中,我重写了 ToString(),如下所示:

namespace DataMining.DataAccess
{
public partial class Transaction
{
    public override string ToString()
    {
      return "some text";
    }

}
}

我现在使用 Transaction,而之前我会使用 MyTransaction

Thanks Marc for pointing me in the right direction. To solve the problem, I did away with the MyTransaction class and in the DataAccess namespace, the one containing the DataContext and Linq-to-SQL classes, I override ToString() as follows:

namespace DataMining.DataAccess
{
public partial class Transaction
{
    public override string ToString()
    {
      return "some text";
    }

}
}

I now use Transaction where I before would have used MyTransaction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文