Getting a NullReferenceException when using dotnet run --project option

682 views Asked by At

I'm trying to run my project file using the dotnet run --project syntax but I get a NullReferenceException as shown:

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

Here's my Main:

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;
}

Using Console.WriteLine(upgrader) prints a DbUp.Engine.UpgradeEngine instance to the console.

I'm using the dbup-core and dbup-sqlserver packages. I'm guessing it's a dependency issue because if I run in within Visual Studio everything works fine.

This is the command I use in Powershell:

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

There are 2 answers

1
Izak Joubert On

For anyone else, downgrading to DBUp 4.2.0 fixed the issue for me

0
Anna Madsen On

DbUp seems to have fixed the issue in v4.5.0. So upgrading the dbup-sqlserver to this version may also fix the exception.