简体   繁体   English

由于SQLite中的Entity Framework Core,SQL插入失败

[英]SQL insert fails with Entity Framework Core in SQLite

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. 我使用带有实体框架核心的移动SQLite数据库。

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? 当我尝试使用Entity Framework Core在数据库中插入数据时,我在做什么错?

The problem was the FormattableString parameter. 问题是FormattableString参数。

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. 看起来,当使用这种类型的字符串时,Entity Framework Core尝试替换插值字符串的变量,但是它无法用无参数 SQL字符串执行此操作,因此失败。

I changed the parameter type to RawSqlString and now the insert is done. 我将参数类型更改为RawSqlString ,现在插入已完成。

Could you include the part where you create: 'command' ? 您能否在其中创建部分:'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. 读取器尝试将每个参数配对以创建最终的SQL查询,但是在名为@ p0的查询中失败。

Disclaimer: I cannot yet write a comment to your question (however I know it would be the best format). 免责声明: 我无法对您的问题发表评论(但是我知道这将是最好的格式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM