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?