【问题标题】:How do I retrieve and set user_version in SQLite DB with EF如何使用 EF 在 SQLite DB 中检索和设置 user_version
【发布时间】:2011-04-06 04:39:56
【问题描述】:

我要做的是从我的 SQLite 数据库中加载并设置 user_version (PRAGMA user_version)。我正在使用实体框架,但如果我不能通过它来做到这一点,那么我需要能够在 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 栏。

【问题讨论】:

    标签: c# sqlite entity-framework-4


    【解决方案1】:

    嗯...我最终想通了。我相信这是使用实体框架时最好的方法。

    要设置 user_version 你需要这样做...

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

    要获取 user_version,您需要这样做...

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

    【讨论】:

    • 在更高版本的 EF 中是 model.Database.SqlQuery&lt;long&gt;("...")
    猜你喜欢
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2015-01-27
    • 2011-02-09
    • 1970-01-01
    • 2014-11-16
    相关资源
    最近更新 更多