无法对LinQ Entity基类进行类型转换

时间:2011-12-20 05:59:28

标签: c#

我使用Visual Studio 2010中的生成器生成了LinQ to SQL实体类。

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

然后我创建了一个Interface ITask

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

最后我创建了我的课程

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

在我的单元测试中的其他地方 我创建了一个myclass对象,发生以下情况

    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

我是C ++程序员,这是我第一次C#任务的一部分。我究竟做错了什么?以上内容在C ++中完全有效。

感谢。

大卫

1 个答案:

答案 0 :(得分:0)

显然,目前不支持继承linq实体类的这个功能,我认为我对OOAD的理解存在缺陷!

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

相关问题