Search code examples
c#sqlsqlitedapperdapper-extensions

Can you tell why DapperExtension's Update method is failing to execute?


I'm using SQLite, Dapper and DapperExtensions, all the latest via NuGet.

Here's my table's schema...

CREATE TABLE `LibraryInfo` (
    `Id`    INTEGER NOT NULL,
    `Name`  TEXT NOT NULL CHECK(length ( Name ) > 0),
    `Description`   TEXT,
    `Version`   TEXT NOT NULL CHECK(length ( Version ) > 0),
    PRIMARY KEY(Id)
);

In it I have a single row with the following data

Id = "0"
Name = "Test Library"
Description = "This is the Test Library"
Version = "1.2.3.4"

Here's my POCO for that table...

public class LibraryInfo
{
    public int    Id          { get; set; }
    public string Name        { get; set; }
    public string Description { get; set; }
    public string Version     { get; set; }
}

Here's how I initialize DapperExtensions (my tables are plural except for this table)...

DapperExtensions.DapperExtensions.DefaultMapper = typeof(PluralizedAutoClassMapper<>);

public class LibraryInfoMapper : ClassMapper<LibraryInfo>
{
    public LibraryInfoMapper()
    {
        Table(nameof(LibraryInfo));
        AutoMap();
    }
}

Here's the code that lets me read that single row with the ID of 0 from the database...

var libraryInfo = connection.Get<LibraryInfo>(0);

That works fine. However, this fails...

libraryInfo.Description = "Test";
connection.Update(libraryInfo);

Here's the exception...

System.Data.SQLite.SQLiteException occurred ErrorCode=1 HResult=-2147467259 Message=SQL logic error or missing database near ".": syntax error Source=System.Data.SQLite StackTrace: at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain) InnerException:

No idea why! I'm trying to figure out how to see what SQL it's generating but I don't know how to do that, or even if it's possible.


Solution

  • The issue is the default dialect for SQL generation within DapperExtensions is SqlServer which means the SQL generated internally is created with table aliases, which Sqlite doesn't support. You need to change the Dapper Extensions SQL dialect to Sqlite before you issue any commands:

    DapperExtensions.DapperExtensions.SqlDialect = new DapperExtensions.Sql.SqliteDialect();
    

    Once this is changed Dapper Extensions will create SQL commands in Sqlite format.