2

I have a rather annoying issue with LinqToSql. I have created 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 :)

Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
  • Can you post the stack trace you get with the exception is thrown? This is the best starting point for figuring out an exception. – Nick Craver Mar 14 '10 at 12:20
  • Sure! InsertOnSubmit(TEntity entity) MDK.Security.Messaging.Message.Send() in E:\Users\Kurt\Documents\Visual Studio 2008\Projects\MDK\MDK.Security\Messaging\Message.cs: line 62 MDK.Tests.MessagingTests.UsersCanSendMessages() in E:\Users\Kurt\Documents\Visual Studio 2008\Projects\MDK\MDK.Tests\MessagingTests.cs: line 89 – Oskar Kjellin Mar 14 '10 at 12:33
  • Duplicates: http://stackoverflow.com/questions/499436/linq-insertonsubmit-nullreferenceexception, among others. They all have one of two solutions, depending on if it's a partial or derived class. – Richard Anthony Hein Mar 14 '10 at 17:39
  • I saw that one Richard Hein, however they either create a partial class(which I do not want to do) or they use ": this()" which I have done without further success – Oskar Kjellin Mar 15 '10 at 08:34

1 Answers1

0

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
Community
  • 1
  • 1
Seba Illingworth
  • 5,672
  • 3
  • 27
  • 25