简体   繁体   English

如何使 Sqlite 在 Windows 10 中工作

[英]How to make SqLite work in Windows 10 at the publication

There is a classic application where Microsoft is used.Entity Framework Core.Sqlite, trying to publish an application through Windows 10 deployment and throws an exception: DllNotFoundException: Unable to load DLL "e_sqlite3": the specified module could not be found.有一个经典的应用,使用了微软的Entity Framework Core.Sqlite,尝试通过Windows 10部署发布应用,抛出异常:DllNotFoundException: Unable to load DLL "e_sqlite3": the specified module could not be found。 (Exception from HRESULT: 0x8007007E) (来自 HRESULT 的异常:0x8007007E)

Tried adding e_sqlite3 library to " Windows application packaging Project", in end face with the exception: SQLite Error 14: 'unable to open database file' with EF Core code first尝试将 e_sqlite3 库添加到“Windows 应用程序打包项目”,最终出现异常:SQLite 错误 14:'无法首先使用 EF Core 代码打开数据库文件

I tried different solutions found on the Internet, but not one I did not fit.我尝试了在 Internet 上找到的不同解决方案,但没有一个我不适合。

Development environment: Windows 10, Visual Studio 2017, Microsoft.EntityFrameworkCore.Sqlite 2.2.1.0 and Microsoft.Data.Sqlite 2.2.1.0开发环境:Windows 10、Visual Studio 2017、Microsoft.EntityFrameworkCore.Sqlite 2.2.1.0和Microsoft.Data.Sqlite 2.2.1.0

Here's how I solved both issues.这是我解决这两个问题的方法。

The first issue is that the native e_sqlite3.dll files are not copied to the Package project's output.第一个问题是原生的 e_sqlite3.dll 文件没有被复制到 Package 项目的输出中。 The Package project has MSBuild logic in Microsoft.DesktopBridge.targets that is calling the GetCopyToOutputDirectoryItems target of each of its referenced projects (eg a WPF project). Package 项目在 Microsoft.DesktopBridge.targets 中有 MSBuild 逻辑,它调用每个引用项目(例如 WPF 项目)的 GetCopyToOutputDirectoryItems 目标。 Since the e_sqlite3.dll files are being included in the referenced project by way of a NuGet package, the way in which they are being included doesn't cause them to be picked up by the GetCopyToOutputDirectoryItems target.由于 e_sqlite3.dll 文件通过 NuGet 包包含在引用的项目中,因此包含它们的方式不会导致它们被 GetCopyToOutputDirectoryItems 目标选中。 I've worked around this by adding the following code to my WPF project:我通过将以下代码添加到我的 WPF 项目来解决这个问题:

  <Target Name="IncludeNativeBinariesAsOutput" BeforeTargets="GetCopyToOutputDirectoryItems">
    <ItemGroup>
      <Content Include="$(OutputPath)\x64\e_sqlite3.dll">
        <Link>x64\e_sqlite3.dll</Link>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
      <Content Include="$(OutputPath)\x86\e_sqlite3.dll">
        <Link>x86\e_sqlite3.dll</Link>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
    </ItemGroup>

    <AssignTargetPath Files="@(Content)" RootFolder="$(MSBuildProjectDirectory)">
      <Output TaskParameter="AssignedFiles" ItemName="ContentWithTargetPath" />
    </AssignTargetPath>
  </Target>

The next issue is with the "unable to open database file" error after the necessary native files are where they need to be.下一个问题是在必要的本机文件位于需要的位置后出现“无法打开数据库文件”错误。 I'm thinking this is because it's trying to create the project in a location that is not supported by a Windows Package project.我认为这是因为它试图在 Windows 包项目不支持的位置创建项目。 I've handled this by setting a special value that that SqliteConnection looks for to construct a path for the database file.我通过设置一个特殊值来处理这个问题,SqliteConnection 会寻找该值来构建数据库文件的路径。 I just added this line to my App constructor class before doing any database operations.在执行任何数据库操作之前,我只是将这一行添加到我的 App 构造函数类中。

AppDomain.CurrentDomain.SetData("DataDirectory", ApplicationData.Current.LocalFolder.Path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM