如何使用EF在SQLite DB中检索和设置user_version

我要做的是加载并从我的SQLite数据库中设置user_version(PRAGMA user_version)。 我正在使用entity framework,但如果我不能通过它实现它,那么我需要能够在C#4.0中以其他方式实现它。

我试过了:

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(); 

其中DatabaseConnection是我的EF上下文,Connection是上下文中的DbConnection对象,但是我得到一个例外:

查询语法无效。 接近标识符’user_version’,第1行,第8列。

嗯……我终于明白了。 我相信这是使用entity framework时最好的方法。

要设置user_version,您需要执行此操作…

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

要获得user_version,您需要这样做……

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