Search code examples
c#sqliteentity-framework-4

How do I retrieve and set user_version in SQLite DB with EF


What I am trying to do is load in and set the user_version (PRAGMA user_version) from my SQLite database. I am using entity framework, but if I can't do it through that, then I need to be able to do it some other way possible within C# 4.0.

I tried:

this.DatabaseConnection.Connection.Open();
System.Data.Common.DbCommand cmd = this.DatabaseConnection.Connection.CreateCommand();
cmd.CommandText = "PRAGMA user_version";
cmd.CommandType = System.Data.CommandType.Text;
System.Data.Common.DbDataReader reader = cmd.ExecuteReader();

where DatabaseConnection is my EF context and Connection is the DbConnection object within the context, but I get an exception:

The query syntax is not valid. Near identifier 'user_version', line 1, column 8.


Solution

  • Well...I eventually figured it out. I believe this is the best way to do it while using entity framework.

    To set the user_version you need to do this...

    this.DatabaseConnection.ExecuteStoreCommand("PRAGMA user_version = 5");
    

    To get the user_version you need to do this...

    long result = this.DatabaseConnection.ExecuteStoreQuery<long>("PRAGMA user_version").FirstOrDefault();