【问题标题】:Getting a NullReferenceException when using dotnet run --project option使用 dotnet run --project 选项时出现 NullReferenceException
【发布时间】:2021-01-11 22:35:37
【问题描述】:

我正在尝试使用 dotnet run --project 语法运行我的项目文件,但我得到了 NullReferenceException,如下所示:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at DbUp.Engine.UpgradeEngine.PerformUpgrade()

这是我的主要内容:

static int Main()
{
    var connectionString =
        "Data Source=.;" +
        "Initial Catalog=MyTable;" +
        "User id=SA;" +
        "Password=<mypasswordhere>;";

    var upgrader =
        DeployChanges.To
            .SqlDatabase(connectionString)
            .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
            .LogToConsole()
            .Build();

    var result = upgrader.PerformUpgrade();

    if (!result.Successful)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(result.Error);
        Console.ResetColor();
        return -1;
    }

    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("Success!");
    Console.ResetColor();
    return 0;
}

使用Console.WriteLine(upgrader)DbUp.Engine.UpgradeEngine 实例打印到控制台。

我正在使用dbup-coredbup-sqlserver 包。我猜这是一个依赖问题,因为如果我在 Visual Studio 中运行一切正常。

这是我在 Powershell 中使用的命令:

dotnet run --project <full path to my .csproj file>

【问题讨论】:

  • 堆栈跟踪真的只有一层深吗(不包括你自己的代码)?在大多数情况下,抛出 NullReferenceException 的代码都有错误。它假设某些东西不为空,而实际上它为空。但是,它可能只是“草率”的参数检查,但您提供的唯一可以为 null 的参数是 Assembly.GetExecutingAssembly()。您应该直接在代码中检查以排除这种可能性,然后与PerformUpgrade 的作者联系。
  • 在运行项目之前尝试运行dotnet restore
  • @MartinLiversage 不,但是其余的只是指向我的项目名称。
  • @PavelAnikhouski 不,很遗憾,这不起作用
  • @MartinLiversage 似乎Assembly.GetExecutingAssembly() 是这里的问题。奇怪的是,在 Visual Studio 中调试时一切正常

标签: .net visual-studio command-line-interface dbup


【解决方案1】:

对于其他人来说,降级到 DBUp 4.2.0 为我解决了这个问题

【讨论】:

  • 我在 dbup-sqlite 中发现了相同或类似的错误。降级只是导致一个关于找不到 sqlite dll 的错误。实际问题是由于我的程序集中缺少 Microsoft.Data.Sqlite nuget 引用。似乎 DbUp 在预防性错误检查/报告方面可能不是最好的。
【解决方案2】:

DbUp 似乎已在 v4.5.0 中解决了该问题。 所以将dbup-sqlserver升级到这个版本也可以修复异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2012-06-25
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多