0

I have generated LinQ to SQL entity classes using the generator in Visual Studio 2010.

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Address")]
    public partial class Address : INotifyPropertyChanging,INotifyPropertyChanged     
   { 
        //all the generated methods are here
   }

Then I create an Interface ITask

  public Interface ITask
  {
      //some interface methods go here
  }

Finally I create my class

class myclass : Address, ITask
{
    //implement interface methods
}

Elsewhere in my unit tests I create a myclass object the following happens

    myclass temp=new myclass();
    Address adr=(Address)temp;
    adr.GetType();//returns myclass when I watch the var in debugger
    DataContext dc=new DataContext();//the linq data context
    dc.Addresses.InsertOnSubmit(adr);//Object reference not set 
    //to an instance of an object NullReferenceException
    ITask task=(ITask)temp;
    temp.Callinterfacemethods();//works fine, iam able to call interface methods

I am a C++ programmer, this is part of my first C# assignment. What am I doing wrong? The above would be perfectly valid in C++.

Thanks.

David

david
  • 485
  • 1
  • 4
  • 10
  • I think you'll find your answer here : [http://stackoverflow.com/questions/499436/linq-insertonsubmit-nullreferenceexception](http://stackoverflow.com/questions/499436/linq-insertonsubmit-nullreferenceexception) – Malcolm O'Hare Dec 20 '11 at 06:39
  • Try to use Address adr = temp as Address; if (adr != null) { doSomething(); }. Please look at this: http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.71%29.aspx – Lucian Dec 20 '11 at 06:41
  • @Malcome O'Hare - That link shows me how to overcome the problem ie extending the partial class.It does not answer why inheriting the class does not work(as explained in my question above). – david Dec 20 '11 at 10:11

1 Answers1

0

Apparently this feature ie inheriting a linq entity class is not supported currently and I was thinking that my understanding of OOAD was flawed!

http://connect.microsoft.com/VisualStudio/feedback/details/310798/linq-to-sql-nullreferenceexception-when-attaching-inherited-record

david
  • 485
  • 1
  • 4
  • 10