在 LINQ to SQL 中执行 InsertOnSubmit 时出现 NullReferenceException
在我的数据库中,我有一个名为 StaffMembers
当我通过 linq-to-sql 将其带入我的 .net 项目时,会创建一个实体类 StaffMember
现在,我还在我的项目中创建了一个部分类 StaffMember,以添加我在其他顶层使用的额外属性。例如。 IsDeleted 属性。该分部类还继承了一个抽象类和接口,以确保还实现了一些其他属性。
现在,当我创建“StaffMember”的新实例时,
例如。 StaffMember newStaff = new StaffMember(); 并为其提供所有属性等
,然后通过我的经理在上下文上调用 InsertOnSubmit。
Add(StaffMember newStaff)
{
context.StaffMembers.InsertOnSubmit(newStaff);
context.Save();
}
我得到一个 “对象引用未设置到对象的实例”错误。
在 context.StaffMembers.InsertOnSubmit(newStaff);
堆栈说
" at System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance)\r\n at
System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.get_HasDeferredLoaders()\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(Object obj, Boolean recurse)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(Object obj)\r\n at System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)\r\n at
BusinessObjects.StaffMemberManager.Add(StaffMember staffMember) in
C:\\StaffMemberManager.cs:line 251"
知道为什么会发生这种情况以及解决方法。
谢谢
In my database I have a table called
StaffMembers
when I bring this into my .net Project as through linq-to-sql an entity class StaffMember is created
Now I have also created a partial class StaffMember in my project also, to add extra properties that I use in other top layers. eg. IsDeleted property. This partial class also inherits an abstract class and interface to make sure some other properties are also implemented.
Now when I create a new instance of "StaffMember"
eg.
StaffMember newStaff = new StaffMember();
and give it all its properties etc
and then call the InsertOnSubmit on the context through my Manager.
Add(StaffMember newStaff)
{
context.StaffMembers.InsertOnSubmit(newStaff);
context.Save();
}
I get an
"Object reference not set to an instance of an object" error.
on context.StaffMembers.InsertOnSubmit(newStaff);
The stack says
" at System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance)\r\n at
System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.get_HasDeferredLoaders()\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(Object obj, Boolean recurse)\r\n at
System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(Object obj)\r\n at System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)\r\n at
BusinessObjects.StaffMemberManager.Add(StaffMember staffMember) in
C:\\StaffMemberManager.cs:line 251"
Any idea why would this be happening and what's the way around it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了答案
http://social.msdn.microsoft .com/Forums/en/linqprojectgeneral/thread/0cf1fccb-6398-4f16-920b-adef9dc4ac9f
以防有些人仍在寻找答案。
当您重载分部类中的构造函数而不调用其中的默认构造函数时,就会出现此问题。
实体的默认构造函数执行 Context 对象所需的一些操作。
因此,如果您的分部类中有一个重载构造函数并使用它来创建对象,请确保
在 C# 的第一行中调用默认构造函数,您可以通过
以下方式执行此操作:
您需要
在 C# 中添加一个,其中 Customer 是我的类,Customer(string custID):this() 是我的部分类中的重载构造函数。
Alright I found my answer on
http://social.msdn.microsoft.com/Forums/en/linqprojectgeneral/thread/0cf1fccb-6398-4f16-920b-adef9dc4ac9f
in case some is still looking for an answer.
This problem happens when you overload the constructor in the partial class, and not call the default constructor in it.
The default constructor of the entity does few things thats required by the Context object.
Hence if you have an overloading constructor in your partial class and using it to create the object, make sure the default constructor is called in the first line
in C# you can do this by
eg.
you need to add a
in C# where Customer is my class and Customer(string custID):this() is my overload constructor in my partial class.
有时,只是忘记将这一行添加到基类中:
[InheritanceMapping(Code = "Class", Type = typeof(Class))]
Sometime, just forgot to add this line to the base class:
[InheritanceMapping(Code = "Class", Type = typeof(Class))]