简体   繁体   English

[LINQ] InsertOnSubmit NullReferenceException

[英][LINQ]InsertOnSubmit NullReferenceException

I have a rather annoying issue with LinqToSql. 我对LinqToSql有一个很烦人的问题。 I have created a class that is derived from the class in the DataContext. 我创建了一个从DataContext中的类派生的类。

The problem is that as soon as I use "InsertOnSubmit(this);" 问题是,一旦我使用“ InsertOnSubmit(this);”, on this derived class I get a NullReferenceException. 在这个派生类上,我得到一个NullReferenceException。

I've seen some people with the same issue. 我见过有些人也有同样的问题。 However they've used a custom constructor and solved the issue by calling ": this()" like this thread http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/0cf1fccb-6398-4f16-920b-adef9dc4ac9f 但是,他们使用了自定义构造函数,并通过调用“:this()”来解决此问题,例如该线程http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/0cf1fccb-6398-4f16 -920b-adef9dc4ac9f

The difference is that I use a default constructor which causes the base constructor to be called so there should not be any problem! 所不同的是,我使用的是默认构造函数,该构造函数会导致调用基本构造函数,因此应该没有任何问题!

Could someone please help me with this, starts to get annoying! 有人可以帮我这个忙,开始变得烦人!

Thanks :) 谢谢 :)

This is one way: https://stackoverflow.com/.... 这是一种方式: https://stackoverflow.com / ....

If you just want to pre-populate some fields, an alternative might be: 如果您只想预填充某些字段,则可以选择以下替代方法:

partial class MyLinqClass {
    string Text = "Default";

    public MyLinqClass AsOne() {
        Text = "One";
        ...
        return this;
    }
}

var x = new MyLinqClass().AsOne();
context.InsertOnSubmit(x); // x is type MyLinqClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM