Howto read PRAGMA user_version?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Howto read PRAGMA user_version?

Post by bairog » Wed 02 Dec 2015 05:35

Hello.
Are there any classes for reading PRAGMA database properties while using Entity Framework Code-First approach (I'm especially interested in user_version)?
And howto read user_version of a given SQLite database file (for example \\NETWORK SHARE\test.db) in situation when I don't use DbContext?

Thank you.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Howto read PRAGMA user_version?

Post by Shalex » Thu 03 Dec 2015 08:59

There are no Entity Framework specific classes for working with user_version pragma. Either create a new ADO.NET connection or access an existing one within a context (var connection = ((IObjectContextAdapter)myDbContext).ObjectContext.Connection;) and execute PRAGMA statements via SQLiteCommand.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Howto read PRAGMA user_version?

Post by bairog » Tue 13 Jun 2017 13:19

Hello again.
I use the following code to read user_version:

Code: Select all

((IObjectContextAdapter)myDbContext).ObjectContext.ExecuteStoreQuery<long>("PRAGMA user_version").FirstOrDefault()
and following code to change user_version:

Code: Select all

((IObjectContextAdapter)myDbContext).ObjectContext.ExecuteStoreCommand("PRAGMA user_version=XX")
It works perfect for one database.
But when I use one main database and several attached (with different user_version value) - how can I read them all (using .ToList() instead of .FirstOrDefault() returns list with only one element for me)?
And change each one separately?
Thank you in advance.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Howto read PRAGMA user_version?

Post by Shalex » Fri 16 Jun 2017 17:44

Here is an SQLite documenation: https://sqlite.org/pragma.html#pragma_user_version.

Please specify the exact PRAGMA command you are using to return a result set of user_version values for all (main and attached) databases.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Howto read PRAGMA user_version?

Post by bairog » Mon 19 Jun 2017 12:38

Thank you.

Code: Select all

((IObjectContextAdapter)myDbContext).ObjectContext.ExecuteStoreQuery<long>("PRAGMA main.user_version").FirstOrDefault();
and

Code: Select all

((IObjectContextAdapter)myDbContext).ObjectContext.ExecuteStoreQuery<long>("PRAGMA AttachedDatabaseName1.user_version").FirstOrDefault();
works perfectly.

Post Reply