1

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>
Izak Joubert
  • 906
  • 11
  • 29
  • Is the stack trace really only one level deep (not including your own code)? In most cases code that throws a `NullReferenceException` has a bug. It assumed something was not null when in fact it was null. However, it might just be "sloppy" argument checking but the only argument that you provide that can be null is `Assembly.GetExecutingAssembly()`. You should check that directly in your code to rule that possibility out and then engage with the author of `PerformUpgrade`. – Martin Liversage Sep 25 '20 at 15:23
  • try to run `dotnet restore` before running the project – Pavel Anikhouski Sep 25 '20 at 19:50
  • @MartinLiversage No, however the rest just points to my project name. – Izak Joubert Sep 27 '20 at 15:59
  • @PavelAnikhouski Nope, unfortunately that did not work – Izak Joubert Sep 27 '20 at 15:59
  • @MartinLiversage Does seem that `Assembly.GetExecutingAssembly()` is the issue here. Strange that everything works fine however when debugging in Visual Studio – Izak Joubert Sep 27 '20 at 16:11
  • Replaced `Assembly.GetExecutingAssembly()` with `AppContext.BaseDirectory` still getting the issue – Izak Joubert Sep 27 '20 at 16:30

2 Answers2

1

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

Izak Joubert
  • 906
  • 11
  • 29
  • I discovered the same, or similar error with dbup-sqlite. Downgrading just resulted in an error about a sqlite dll not being found. The actual problem was due to a missing Microsoft.Data.Sqlite nuget reference in my assembly. It seems that DbUp may not be the greatest at precautionary error checking / reporting. – nullPainter Oct 03 '20 at 08:03
0

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

Anna Madsen
  • 384
  • 3
  • 15