Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET 4.7.2类库中使用EF Core 3.1 SQLite数据库_C#_.net_Entity Framework Core - Fatal编程技术网

C# 在.NET 4.7.2类库中使用EF Core 3.1 SQLite数据库

C# 在.NET 4.7.2类库中使用EF Core 3.1 SQLite数据库,c#,.net,entity-framework-core,C#,.net,Entity Framework Core,我在.NET 4.7.2项目中包含了包Microsoft.EntityFrameworkCore.Sqlite(最新的3.1.x版本)。 这是我的DbContext的onconfigurang方法: protected override void OnConfiguring(DbContextOptionsBuilder options) { options.UseSqlite($@"Data Source=D:\MyDb.db"); } 我引用了另一个.NET4.

我在.NET 4.7.2项目中包含了包Microsoft.EntityFrameworkCore.Sqlite(最新的3.1.x版本)。 这是我的
DbContext
onconfigurang
方法:

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
    options.UseSqlite($@"Data Source=D:\MyDb.db");
}
我引用了另一个.NET4.7.2项目中的类库

每当我尝试读取、写入或创建数据库时(如
dbContext.database.EnsureCreated();
),我都会在
SqliteConnection
上获得以下
TypeInitializationException
构造函数,其中包含以下InnerException:

消息:
路径的格式无效

堆栈跟踪:

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()
我猜路径与我的连接字符串有关,但我不知道它有什么问题

出于测试目的,我创建了一个.NET5项目,并从中引用了4.7.2类库。如果我调用
dbContext.Database.recreated()
在此项目中,它按预期工作,并创建数据库

所以我想这是一个关于.NET4.7.2和EF核心混合的问题。但是根据EF,Core3.1应该可以很好地与.NET4.7.2配合使用


可能是EF Core的SQLite适配器出了问题?

类型初始化异常误导了我。最后,我在.NET4.7.2项目中使用的
Costura.Fody
包出现了问题,它将所有DLL捆绑到一个*.exe文件中

这似乎不适合SQLite库。无论出于何种原因,
SQLitePCLRaw.*.dll
文件未包含在内,这导致了所述的
TypeInitializationException
。通常,如果不包括DLL,则会得到一个
FileNotFoundException

请看一个类似的问题,不幸的是,我在这里发布了我的问题后才发现这个问题