SQL insert fails with Entity Framework Core in SQLite

c# entity-framework-core sqlite

Question

I have the following insert script:

INSERT INTO Sections (ID, Name, Type, Notes, CodeI, CodeII, DataID, CodeIII, CodeIV, ShopID) 
VALUES ("787c4d0b-8b6e-4bed-8fb7-03932d0346cd", "Test shop", "R", NULL, "123456789", "1234", NULL, "11", "1234", NULL);

I use a mobile SQLite database with Entity Framework Core.

This is my executer method:

public async Task ExecuteCommand(FormattableString command)
{
  if (command == null)
    throw new ArgumentNullException(nameof(command));
  try
  {
    await dbContext.Database.ExecuteSqlCommandAsync(command);
  }
  catch (Exception ex)
  {
    throw;
  }
}

This is the calling code:

var command = $"INSERT INTO Sections (ID, Name, Type, Notes, CodeI, CodeII, DataID, CodeIII, CodeIV, ShopID) 
VALUES ('787c4d0b-8b6e-4bed-8fb7-03932d0346cd', 'Test shop', 'R', NULL, '123456789', '1234', NULL, '11', '1234', NULL);"
await ExecuteCommand(command);

When I execute the following:

 await dbContext.Database.ExecuteSqlCommandAsync(command);

I get this exception:

Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'near "@p0": syntax error'.
  at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC (System.Int32 rc, SQLitePCL.sqlite3 db) [0x00067] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at Microsoft.Data.Sqlite.SqliteCommand+<PrepareAndEnumerateStatements>d__62.MoveNext () [0x0008a] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior) [0x0025d] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader () [0x00000] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery () [0x00042] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at System.Data.Common.DbCommand.ExecuteNonQueryAsync (System.Threading.CancellationToken cancellationToken) [0x00049] in <6409c8a079bb4f4c8dff9761b9062573>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0 
  at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand+<ExecuteAsync>d__17.MoveNext () [0x00358] in <e12f0cc891a249419803faaf433b12e6>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0 
  at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions+<ExecuteSqlCommandAsync>d__13.MoveNext () [0x00154] in <e12f0cc891a249419803faaf433b12e6>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0 
  at MyXamarinTest.ExecuteCommand(FormattableString command)

What am I doing wrong when I try to insert data in my database with Entity Framework Core?

1
0
9/20/2018 9:57:38 PM

Accepted Answer

The problem was the FormattableString parameter.

It appears that when this type of string is used, Entity Framework Core tries to substitute the variables of the interpolated string, but it couldn't do that with a parameterless SQL string, so it failed.

I changed the parameter type to RawSqlString and now the insert is done.

2
9/24/2018 7:24:16 AM

Popular Answer

Could you include the part where you create: 'command' ?

Explanation: From the stack it seems like your parameters are wrongly addressed. The reader tries to pair every parameter up to create the final SQL query, but it fails at one called @p0.

Disclaimer: I cannot yet write a comment to your question (however I know it would be the best format).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow