0

I have a WPF application that is supposed to connect to an sqlite db.

Here is my ContextFactory:

 public class DataContextFactory : IDesignTimeDbContextFactory<DataDbContext>
{
    public DataContextFactory()
    {
        
    }

    public DataDbContext CreateDbContext(string[] args)
    {
        //For migrations
        //Use Environment Variables instead of appsettings
        //Debugger.Launch();
        var builder = new DbContextOptionsBuilder<DataDbContext>();
        var cs = @"Filename=medicalrecalculation.sqlite;";
        builder.UseSqlite(cs);
        return new DataDbContext(builder.Options);
    }
}

This is my DbContext:

public class DataDbContext : Microsoft.EntityFrameworkCore.DbContext
{
    public DataDbContext(DbContextOptions<DataDbContext> options) : base(options)
    {
        ChangeTracker.AutoDetectChangesEnabled = false;
    }
    public DbSet<LOB001MedicalAdditionalProcessingData> LOB001MedicalAdditionalProcessingData { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=medicalrecalculation.sqlite");
    }
    //Seed must contain static data to prevent migrations from updating
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

    }

}

I am able to create migrations using the cli like this:

Add-Migration BaseMigration -Context DataDbContext -OutputDir Migrations

However, when I run Update-Database -Context DataDbContext, I get this error:

Build started... Build succeeded. System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Data.Sqlite.SqliteConnection.Open() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists() at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists() at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Object reference not set to an instance of an object.

I have tried adjusting the connection string and even creating an empty .sqlite file but it still shows this error.

ASh
  • 34,632
  • 9
  • 60
  • 82
JianYA
  • 2,750
  • 8
  • 60
  • 136
  • No? I'm asking why I'm getting an object reference error when I run a command? Nothing in my code is null. – JianYA Mar 10 '22 at 07:43

1 Answers1

0

I found the answer here. Why do I get Object reference not set to an instance of an object when running Update-Database

Turns out I needed both EntityFramework.Core.Sqlite and EntityFramework.Sqlite.Core

JianYA
  • 2,750
  • 8
  • 60
  • 136
  • No, you only need a reference to [`Microsoft.EntityFramework.Core.Sqlite`](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/7.0.0-preview.1.22076.6), because it includes the reference to `Microsoft.EntityFramework.Core.Sqlite.Core` which [README](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite.Core/) tells you *This package does not include a copy of the native SQLite library.* and that is the main problem – Sir Rufo Mar 10 '22 at 08:54