如何使用EF检索和设置SQLite DB中的user_version [英] How do I retrieve and set user_version in SQLite DB with EF
问题描述
我想要做的是加载并从我的SQLite数据库设置user_version(PRAGMA user_version)。我正在使用实体框架,但是如果我不能通过它实现,那么我需要能够在C#4.0中做一些其他方法。
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.
我尝试:
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对象,但是我得到一个异常:
where DatabaseConnection is my EF context and Connection is the DbConnection object within the context, but I get an exception:
查询语法无效。接近
标识符'user_version',第1行,
列8。
The query syntax is not valid. Near identifier 'user_version', line 1, column 8.
推荐答案
嗯,我终于明白了。我相信这是使用实体框架时最好的方法。
Well...I eventually figured it out. I believe this is the best way to do it while using entity framework.
要设置user_version,您需要执行此操作...
To set the user_version you need to do this...
this.DatabaseConnection.ExecuteStoreCommand("PRAGMA user_version = 5");
要获取user_version,您需要执行此操作...
To get the user_version you need to do this...
long result = this.DatabaseConnection.ExecuteStoreQuery<long>("PRAGMA user_version").FirstOrDefault();
这篇关于如何使用EF检索和设置SQLite DB中的user_version的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!