2

I'm trying to run EF6 Code First with SQLite (version 3.11) in Ubuntu/Mono.

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; }
}

User.cs:

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

    public string Nome { get; set; }
}

Whenever it gets to db.SaveChanges(), it throws the following exception:

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 ---

Does anyone know why this is happening?

UPDATE

I managed to find out what was going on. It looks like that the database created was read-only. All I had to do was change its permissions and it worked!

Glund
  • 507
  • 1
  • 5
  • 20
  • it's auto generated SQL codebehind functions that are not implemented i think, not sure though... Take a look at this : http://stackoverflow.com/questions/20460357/problems-using-entity-framework-6-and-sqlite – Antoine Pelletier Nov 28 '16 at 18:55

1 Answers1

2

We managed to solve the problem!

What we did was download the full source of SQLite (sqlite-netFx-full-source-1.0.104.0.zip)and them recompiled SQLite.csproj and EF6.csproj to not use InteropDll using the following commands and specified their platform:

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

And included both dlls in the project, along with Entity's dll.

Then we updated the 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>
Glund
  • 507
  • 1
  • 5
  • 20