【发布时间】: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-core 和dbup-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