0

I have included package Microsoft.EntityFrameworkCore.Sqlite (latest 3.1.x version) in a .NET 4.7.2 Project. This is the OnConfiguring Method of my DbContext:

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
    options.UseSqlite($@"Data Source=D:\MyDb.db");
}

I reference the class library from another .NET 4.7.2 project.

Whenever I try to read from, write to or create the database (like dbContext.Database.EnsureCreated();), I get the following TypeInitializationException on SqliteConnection constructor with the following InnerException:

Message: The path has an invalid format

StackTrace:

at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
   at System.IO.Path.InternalGetDirectoryName(String path)
   at SQLitePCL.NativeLibrary.MakePossibilitiesFor(String basename, Assembly assy, Int32 flags, LibSuffix suffix)
   at SQLitePCL.NativeLibrary.MyLoad(String basename, Assembly assy, Int32 flags, Action`1 log)
   at SQLitePCL.NativeLibrary.Load(String libraryName, Assembly assy, Int32 flags)
   at SQLitePCL.Batteries_V2.MakeDynamic(String name, Int32 flags)
   at SQLitePCL.Batteries_V2.DoDynamic_cdecl(String name, Int32 flags)
   at SQLitePCL.Batteries_V2.Init()

I guess path is related to my ConnectionString but I don't see what's wrong with it.

For testing purposes I have created a .NET 5 Project and refereced the 4.7.2 class library from there. If I call dbContext.Database.EnsureCreated(); In this project, it works as expected and the database is created.

So I guess it is an issues regarding the mix of .NET 4.7.2 and EF Core. But according to Microsoft Docs EF Core 3.1 should work fine with .NET 4.7.2

Maybe something with the SQLite Adapter of EF Core?

Lightbringer
  • 765
  • 1
  • 4
  • 16

1 Answers1

0

The TypeInitializationException mislead me in the wrong direction. In the end, it was a problem with the Costura.Fody package I'm using in the .NET 4.7.2 Project to bundle all DLLs into one *.exe file.

Seems this does not work well with SQLite Libraries. For whatever reason the SQLitePCLRaw.*.dll files were not included which resulted in the described TypeInitializationException. Usually, one would get a FileNotFoundException if a DLL is not included.

See this Answer on a similar question that unfortunately I only found after I had posted my question here.

Lightbringer
  • 765
  • 1
  • 4
  • 16