开发者

[LINQ]InsertOnSubmit NullReferenceException

开发者 https://www.devze.com 2022-12-22 17:43 出处:网络
I have a rather annoying issue with LinqToSql. I have create开发者_StackOverflow中文版d a class that is derived from the class in the DataContext.

I have a rather annoying issue with LinqToSql. I have create开发者_StackOverflow中文版d a class that is derived from the class in the DataContext.

The problem is that as soon as I use "InsertOnSubmit(this);" on this derived class I get a 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

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/....

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号