entity-framework - SQL逻辑错误或缺少数据库没有这样的功能: last_rows_affected

标签 entity-framework ubuntu sqlite mono ef-code-first

我正在尝试在 Ubuntu/Mono 中使用 SQLite(版本 3.11)运行 EF6 Code First。

Program.cs:

    static void Main(string[] args)
    {
        var db = new Context();
        var user = new User() { Nome = "Teste"};

        db.User.Add(user);

        db.SaveChanges();

    }

Context.cs:

public class Context : DbContext
{
    public Context():base("Context")
    {
        Database.SetInitializer<Context>(new CreateDatabaseIfNotExists<Context>());
    }
    public DbSet<User> User { get; set; }
}

用户.cs:

[Table("User")]
public class User
{
    [Key]
    public int Id { get; set; }

    public string Nome { get; set; }
}

每当到达 db.SaveChanges() 时,它都会抛出以下异常:

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SQLite.SQLiteException: SQL logic error or missing database
no such function: last_rows_affected
  at System.Data.SQLite.SQLite3.Prepare (System.Data.SQLite.SQLiteConnection cnn, System.String strSql, System.Data.SQLite.SQLiteStatement previous, System.UInt32 timeoutMS, System.String& strRemain) [0x0051b] in <2502d764dcbe41f1ad84e79b77538a55>:0
  at System.Data.SQLite.SQLiteCommand.BuildNextCommand () [0x0007c] in <2502d764dcbe41f1ad84e79b77538a55>:0
  --- End of inner exception stack trace ---
  at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update () [0x00080] in <ba0120930fe443a3b992bc3dba4c985a>:0
  at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2 (System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator ut) [0x00000] in <ba0120930fe443a3b992bc3dba4c985a>:0
  at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T] (T noChangesResult, System.Func`2[T,TResult] updateFunction) [0x00063] in <ba0120930fe443a3b992bc3dba4c985a>:0
  at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update () [0x00000] in <ba0120930fe443a3b992bc3dba4c985a>:0
  at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__d () [0x00000] in <ba0120930fe443a3b992bc3dba4c985a>:0
  at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T] (System.Func`1[TResult] func, System.Data.Entity.Infrastructure.IDbExecutionStrategy executionStrategy, System.Boolean startLocalTransaction, System.Boolean releaseConnectionOnSuccess) [0x00064] in <ba0120930fe443a3b992bc3dba4c985a>:0
  --- End of inner exception stack trace ---

有谁知道为什么会这样吗?

更新

我设法找出发生了什么事。看起来创建的数据库是只读的。我所要做的就是更改其权限,结果成功了!

最佳答案

我们成功解决了问题!

我们所做的是下载 SQLite 的完整源代码 (sqlite-netFx-full-source-1.0.104.0.zip),然后使用以下命令重新编译 SQLite.csproj 和 EF6.csproj 以不使用 InteropDll 并指定其平台:

xbuild /p:Configuration=Release /p:UseInteropDll=false /p:UseSqliteStandard=true System.Data.SQLite/System.Data.SQLite.2010.csproj


xbuild /p:Configuration=Release System.Data.SQLite.Linq/System.Data.SQLite.EF6.2010.csproj /p:Platform=x64

并将这两个 dll 以及 Entity 的 dll 都包含在项目中。

然后我们更新了 app.config:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <connectionStrings>
    <add name="Context" connectionString="data source=teste.s3db;" providerName="System.Data.SQLite.EF6" /> 
   </connectionStrings>

 <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.104.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
  </system.data>    
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.104.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </providers>
  </entityFramework>
</configuration>

关于entity-framework - SQL逻辑错误或缺少数据库没有这样的功能: last_rows_affected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850925/

相关文章:

vb.net - 从 LINQ 查询收集的返回类型

c# - Entity Framework 6 SaveChanges() 覆盖不一致地检测更改

c# - 如何将表达式 Func<T,int> 转换为 Func<T,object>

c# - 如何模拟返回的模拟对象的方法?

php - 在 Linux 上的 Lighttpd 上运行 Rails 和 PHP

Android SQLite 文件加密

java - 从 listView 获取 textView 值

linux - `!:-` 是做什么的?

javascript - 如何在本地项目中使用jsdoc

java - 哪个更快 - Cursor 或 ArrayList?