Washington A. Ramos
Washington A. Ramos

Reputation: 924

Using a SQLite + Entity Framework with a pre-populated database

I've followed the tutorial at https://ef.readthedocs.io/en/staging/platforms/uwp/getting-started.html on how to create & use a local SQLite database. Everything works just fine and I can do all the operations, but I'd like to use a pre-populated database.

How should I proceed to load a pre-populated database.db file (Which would be in my Assets folder) and use it in my app?

Upvotes: 0

Views: 322

Answers (1)

Dave Smits
Dave Smits

Reputation: 1879

You can put a db file in your appx; on first run copy this db file to the local storage? Simply check if the file is already in the local appdata and if not copy from your appx to the localstorage.

Bit like

private async Task EnsureDatabaseAsync()
        {
                var localFolder = ApplicationData.Current.LocalFolder;
            try
            {
                var localDbFile = await localFolder.GetFileAsync("mydb.db");
            }
            catch(FileNotFoundException)
            {
                var dbFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("mx-appx:///local/assets/mydb.db"));
                await dbFile.CopyAsync(localFolder);
            }
        }

Upvotes: 3

Related Questions