我正在尝试创建一些方法(GetContent 和 SaveContent)来扩展 linq2sql DataClass
显然我在滥用“this”,因为代码在 dbo.docs.InsertOnSubmit(this); 上失败了。
我的测试代码是这样的:
[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); 生成“预期标识符,基数是关键字”错误
(没有足够的声誉点来发布图片)