C# 中的 LINQ DataContext.SubmitChanges() 报告“未将对象引用设置为对象的实例。”

发布于 2024-08-06 09:31:56 字数 809 浏览 6 评论 0原文

我认为很多人都遇到过这个问题,但我无法解决它或理解为什么我会遇到这个问题。我已经把头发扯了几个小时了。

我收到错误“未将对象引用设置为对象的实例”。在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2024-08-13 09:31:56

不要将您的类型之一称为 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... (or System, 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 become null), and you don't indicate if (for example) you have added any partial methods to the data-context or entity, or have any events. I would expect the problem to be in one of those areas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文