Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite database created in wrong directory in packaged desktop app. #24213

Closed
sjb-sjb opened this issue Feb 22, 2021 · 16 comments · Fixed by #26114
Closed

SQLite database created in wrong directory in packaged desktop app. #24213

sjb-sjb opened this issue Feb 22, 2021 · 16 comments · Fixed by #26114
Assignees
Labels
area-platform area-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@sjb-sjb
Copy link

sjb-sjb commented Feb 22, 2021

When using SQLite with EfCore under UWP, new SQLite databases are created in ApplicationData.Current.LocalFolder, in other words [user]\AppData\Local\Packages\[package number]\LocalState.

However, when I compiled my program as a packaged desktop app, the SQLite database was then created in [user]\AppData\Local\VirtualStore\Windows\SysWOW64. I am using WinUI 3 preview 4 with .NET 5 and VS 16.9.0 preview 4, and I created my app with the "Blank App, Packaged (WinUI in Desktop)" template that comes with the WinUI 3 preview 3 extension. I am creating my database using EnsureCreatedAsync() and I am using Microsoft.EntityFrameworkCore.Sqlite v5.0.3.

You may recall that #19754 and #19468 were earlier bugs in which EF did not initialize the SQLite directory. The workaround was to insert the following lines prior to using SQLite:

const int dataDirectoryType = 1;
const int tempDirectoryType = 2;
SQLitePCL.Batteries_V2.Init();
SQLitePCL.raw.sqlite3_win32_set_directory( dataDirectoryType, Windows.Storage.ApplicationData.Current.LocalFolder.Path);
SQLitePCL.raw.sqlite3_win32_set_directory( tempDirectoryType, Windows.Storage.ApplicationData.Current.TemporaryFolder.Path);

When I applied this workaround to my packaged desktop app, the SQLite database was then created in ApplicationData.Current.LocalFolder.

P.S. See also Eric Sink's entry in the SQLite repo

@bricelam
Copy link
Contributor

bricelam commented May 5, 2021

I suspect this line needs updating for WinUI 3 Desktop apps.

return Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")

@sjb-sjb
Copy link
Author

sjb-sjb commented Jul 4, 2021

Can you suggest when this might make it into a release?

@bricelam
Copy link
Contributor

bricelam commented Jul 6, 2021

image

@sjb-sjb
Copy link
Author

sjb-sjb commented Jul 7, 2021

Thanks!

bricelam added a commit to bricelam/efcore that referenced this issue Sep 20, 2021
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 20, 2021
@bricelam
Copy link
Contributor

@sjb-sjb I get SQLITE_ERROR from sqlite3_win32_set_directory when I try your workaround.

@bricelam
Copy link
Contributor

@ericsink I think the UWP native assets for e_sqlite3.dll should be used in this case, but I'm not sure NuGet can do that...

@bricelam bricelam added area-xamarin and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Sep 20, 2021
@bricelam bricelam removed this from the 6.0.0 milestone Sep 20, 2021
@bricelam
Copy link
Contributor

@marb2000 Do you know anyone that can help with NuGet-packaging a library to work with the new stack? The package currently looks like this to get a different native library on UWP, but this falls down in the new world of net5.0-windows TFMs.

sqlitepclraw.lib.e_sqlite3.2.0.6.nupkg
└───runtimes
    ├───win-x64
    │   └───native
    │           e_sqlite3.dll
    │
    └───win10-x64
        └───nativeassets
            └───uap10.0
                    e_sqlite3.dll

@ericsink
Copy link

I'm not caught up yet on this current incarnation of this issue. Is the problem that the wrong native DLL is getting deployed?

@sjb-sjb
Copy link
Author

sjb-sjb commented Sep 21, 2021

@bricelam Have not compiled and run for some time now due to an extensive set of changes in progress, so it is difficult for me to confirm much right now. Broadly I am using WinUI desktop, Net 5, EF Core 5.

@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Sep 21, 2021
bricelam added a commit to bricelam/efcore that referenced this issue Sep 21, 2021
@bricelam
Copy link
Contributor

So, I've opened a PR that enables Microsoft.Data.Sqlite to call sqlite3_win32_set_directory in projects targeting net6.0-windows10.0.19041 (or similar)

This still doesn't work when using e_sqlite3.dll since the API returns SQLITE_ERROR. I suspect it's just deploying the wrong (i.e. non-appcontainer) library.

I've verified that the fix works correctly with winsqlite3.dll.

@bricelam bricelam added closed-external closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Sep 21, 2021
@ericsink
Copy link

I suspect it's just deploying the wrong (i.e. non-appcontainer) library.

Maybe. Probably.

But it's also the case that in non-UWP builds of SQLitePCLRaw, sqlite3_win32_set_directory is a stub that returns SQLITE_ERROR.

@bricelam
Copy link
Contributor

bricelam commented Sep 21, 2021

Ah, you're right! Using the UWP provider assembly makes it all work. (with my PR changes)

<ItemGroup>
  <PackageReference Include="SQLitePCLRaw.provider.e_sqlite3" Version="2.0.6"  ExcludeAssets="All" GeneratePathProperty="True" />
  <Reference Include="$(PkgSQLitePCLRaw_provider_e_sqlite3)\lib\uap10.0\SQLitePCLRaw.provider.e_sqlite3.dll" />
</ItemGroup>

@ericsink
Copy link

@bricelam So... is there some change I need to make? Seems like the handling of things like net6.0-windows10.0.19041 needs to change in my nuget packages?

@bricelam
Copy link
Contributor

Yes, something in the provider package needs to change… I’m just not sure what. Maybe multi-target net5.0-windows in addition to uap10.0? I’m not sure how MSIX-packaging, appcontainers, and NuGet all interact to pick the assets. I haven’t seen any official guidance yet, but it was pretty sparse in UWP too.

@ericsink
Copy link

Acknowledged. I'll try to figure this out.

@sjb-sjb
Copy link
Author

sjb-sjb commented Dec 13, 2021

Under UWP it was not possible to create the SQLite database in any other directory. Is it now possible to create the SQLite database in the user's Documents directory when using a packaged app based on the Windows App SDK, WinUI 3, and .NET 6?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-platform area-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants