简体   繁体   English

实体框架的问题

[英]Problems with entity framework

I'm having problems working with EntityFramework.我在使用 EntityFramework 时遇到问题。 While below peace of code works fine on my PC, when it's transported to a VPS (with everything properly preinstalled), it gives me a Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object, but the message box that is supposed to catch this does not show up.虽然低于和平代码在我的 PC 上工作正常,但当它被传输到 VPS(所有东西都正确预装)时,它给了我一个未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例,但消息框应该抓住这个没有出现。 Any suggestions?有什么建议?

Thank you in advance.先感谢您。

        var cc = new CopierContext();
        try
        {
            MessageBox.Show(cc.Database.Connection.ConnectionString.ToString());

            var matchingProviders2 = cc.Providers.Where(prov => prov.Login == "batman");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.InnerException.Message);
        }

Update:更新:

I finally got to the core of the problem.我终于找到了问题的核心。 The reason is: I've had .NET 4 on VPS, while application was developed using .NET 4.5.原因是:我在 VPS 上安装了 .NET 4,而应用程序是使用 .NET 4.5 开发的。 Installing the latter one removed all problems.安装后一个消除了所有问题。 Thank you for all your help.谢谢你的帮助。

我不知道你是如何在你的 VPS 上运行它的,但如果它不是在交互式用户帐户下启动的,你的消息框将不会显示。

Well from the docs on the Exception.InnerException Property来自Exception.InnerException属性的文档

The InnerException property returns the same value as was passed into the constructor, or a null reference InnerException 属性返回与传递给构造函数的值相同的值,或空引用

Since you're catching any old exception catch (Exception e) its quite possible that the exception that's being thrown isn't the exception you were expecting and doesn't have a InnerException.由于您正在捕获任何旧的异常catch (Exception e) ,因此抛出的异常很可能不是您期望的异常并且没有 InnerException。 This means your catch block may be raising an exception.这意味着您的 catch 块可能会引发异常。

There are several actions you could take.您可以采取多种措施。

  1. Do not catch System.Exception exception in anything but a top level exception handler.除了顶级异常处理程序之外,不要在任何东西中捕获 System.Exception 异常。 Only catch exceptions you know what to do with.只捕获您知道如何处理的异常。 Which leads to...这导致...
  2. Set up a top level exception handler设置顶级异常处理程序
  3. Finally when logging or displaying exception messages at least make sure you have an inner exception before you try and use it.最后,在记录或显示异常消息时,至少要确保在尝试使用它之前有一个内部异常。

    MessageBox.Show( (e.InnerException != null ? e.InnerException : e).Message );

Don't use a MessageBox as it is a service;不要使用 MessageBox,因为它是一种服务; log them to disk instead such that you can recall them, or perhaps automatically mail them to you such that you are up to date on problems occuring.将它们记录到磁盘,以便您可以调用它们,或者自动将它们邮寄给您,以便您及时了解发生的问题。

You will also want to add e.InnerException.StackTrace to the log.您还需要将e.InnerException.StackTrace添加到日志中。

My bet is that you did not configure something (or did not configure it correctly) and cc.Database.Connection.ConnectionString is null.我敢打赌,您没有配置某些内容(或没有正确配置)并且cc.Database.Connection.ConnectionString为空。 Calling .ToString() causes the NullReferenceException.调用.ToString()会导致 NullReferenceException。 That's why you don't see the message box.这就是您看不到消息框的原因。 As other people said - using MessageBox for this kind of debugging is not a good idea.正如其他人所说 - 使用 MessageBox 进行这种调试不是一个好主意。 In the catch you should print not message but e.ToString() it will show the stack trace that should point to the place where the problem is.在捕获中,您不应该打印消息,而是打印 e.ToString() 它将显示应指向问题所在位置的堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM