本文介绍了在LINQ做InsertOnSubmit时的NullReferenceException到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库我有一个表称为
StaffMembers

In my database I have a table calledStaffMembers

当我通过LINQ到SQL实体类带进我的.NET项目这是StaffMember创建

when I bring this into my .net Project as through linq-to-sql an entity class StaffMember is created

现在我也曾在我的项目也创造了部分类StaffMember,补充一点,我在其他上层使用额外的属性。例如。物业请将isDeleted。这部分类还继承抽象类和接口,以确保其他一些特性也可以实现。

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.

现在,当我创建StaffMember

Now when I create a new instance of "StaffMember"

如。
StaffMember NEWSTAFF =新StaffMember();
,并给它所有的属性等。

eg.StaffMember newStaff = new StaffMember();and give it all its properties etc

,然后调用InsertOnSubmit通过我的经理的背景。

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.

在context.StaffMembers.InsertOnSubmit(NEWSTAFF);

on 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"

任何想法,为什么会这样发生的,什么是它周围的方式。

Any idea why would this be happening and what's the way around it.

感谢

推荐答案

好吧,我发现我的

在情况下,一些仍然在寻找一个答案。

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

在C#中你可以这样做通过

in C# you can do this by

如:

 Customer(string custID)

您需要添加一个

 Customer(string custID):this()

在C#如果客户是我的课堂和客户(字符串客户ID):这个()是在我的部分类我的过载构造

in C# where Customer is my class and Customer(string custID):this() is my overload constructor in my partial class.

这篇关于在LINQ做InsertOnSubmit时的NullReferenceException到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:09