C# 中的 LINQ DataContext.SubmitChanges() 报告“未将对象引用设置为对象的实例。”
我认为很多人都遇到过这个问题,但我无法解决它或理解为什么我会遇到这个问题。我已经把头发扯了几个小时了。
我收到错误“未将对象引用设置为对象的实例”。在我的 datacontext.SubmitChanges() 上,该方法第二次运行(我正在循环一组 ObjectName 字符串):
private Object CreateObject(string ObjectName, SystemClassEnum SystemClass)
{
Object result = new Object();
result.Name = ObjectName;
result.SystemClassID = (int)SystemClass;
_dataContext.Objects.InsertOnSubmit(result);
_dataContext.SubmitChanges();
return result;
}
我认为这是因为 result.Name 值可以为 null,但我不再这样认为了。
似乎 dataContext 正在关闭?但在调试模式下,我检查连接的状态,错误发生后它是“打开”的。
我正在使用存储库模式和 ASP.NET MVC。
结果对象 (new Object()) 是 LINQ DBML 自动生成的类的实例,该类还具有我使用附加的单个扩展方法创建的部分类。我不明白类的扩展如何导致问题。
我没主意了。
有什么想法吗?感谢您提供的任何帮助!
最好的问候,
埃里克
I think a lot of people have had this issue but I'm not able to fix it or understand why I'm having it. I've been tearing hair out for a couple of hours now.
I'm getting the error, "Object reference not set to an instance of an object." on my datacontext.SubmitChanges() on the SECOND time this method runs (I'm looping through a set of ObjectName strings):
private Object CreateObject(string ObjectName, SystemClassEnum SystemClass)
{
Object result = new Object();
result.Name = ObjectName;
result.SystemClassID = (int)SystemClass;
_dataContext.Objects.InsertOnSubmit(result);
_dataContext.SubmitChanges();
return result;
}
I thought it was because the result.Name value can be null but I don't think that anymore.
It seems like somehow the dataContext is getting closed? but in Debug Mode I check the status of the connection and it's "Open" after the error occurs.
I'm using the repository pattern and ASP.NET MVC.
The result object (new Object()) is an instance of a LINQ DBML auto-generated class that also has a partial class that I created with a single extension method attached. I don't see how the extension to the class could be causing the problem.
I'm out of ideas.
Any thoughts? Thanks for any help you can provide!!
Best regards,
Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将您的类型之一称为
Object
- 这是一个非常糟糕的主意;你会后悔的...选择一个不同的名称...(或System
等)。您确定错误实际上不在上面的行中(
_dataContext.Objects.
等)?不幸的是,您没有显示任何与_dataContext
相关的代码(例如,它是否会变成null
),并且您没有指出是否(例如)您已将任何partial
方法添加到数据上下文或实体,或者有任何事件。我预计问题出在这些领域之一。Don't call one of your types
Object
- that is a seriously bad idea; you will regret it... pick a different name... (orSystem
, etc).Are you sure the error isn't actually on the line above (
_dataContext.Objects.
etc)? Unfortunately, you don't show any of the code relating to_dataContext
(for example, could it have becomenull
), and you don't indicate if (for example) you have added anypartial
methods to the data-context or entity, or have any events. I would expect the problem to be in one of those areas.