如何使用linq2sql表作为基类

时间:2013-07-08 17:25:09

标签: c# .net linq unit-testing

我正在尝试创建一些方法(GetContent和SaveContent)来扩展linq2sql DataClass

显然我误用了“这个”,因为代码失败了     dbo.docs.InsertOnSubmit(本);

我的测试代码是这样的:

[TestMethod]
public void TestMethod1()
{
    var newProfile = new profile();
    newProfile.GetContent();
    newProfile.saveProfile();

}
像这样的类库:

namespace ClassLibrary1
{
    public partial class doc // extends class created by linq2sql
    {
        public int doc_ii { get; set; }    
    }
    public class profile : doc  // extends doc class with get and save
    {
        public void GetContent()
        {
            this.url = "http://";
            this.content = "<html><head>...</head>,</html>";
        }
        public void SaveProfile()
        {
            var dbo = new DataClasses1DataContext();
                                            // NullReference
            dbo.docs.InsertOnSubmit(this);  // not set to an instance of an object.
                                            //
            dbo.SubmitChanges();
        }
    }
}

例外细节

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=System.Data.Linq
  StackTrace:
       at System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)
       at ClassLibrary1.profile.SaveProfile() in t:\TRACKER_sln\ClassLibrary1\ClassLibrary1\profile.cs:line 26
       at UnitTestProject1.UnitTest1.TestMethod1() in t:\TRACKER_sln\ClassLibrary1\UnitTestProject1\UnitTest1.cs:line 15
  InnerException: 

“下面的图像”中的本地化表示基类被包装为this.base,但是dbo.docs.InsertOnSubmit(this.base);生成“预期的标识符,base是关键字”错误

(没有足够的声望点发布图片)

0 个答案:

没有答案