【问题标题】:Entity Framework Core ExecuteSqlCommand delete with SQLite does not work使用 SQLite 删除的 Entity Framework Core ExecuteSqlCommand 不起作用
【发布时间】:2019-04-16 14:33:42
【问题描述】:

我有以下型号:

public class LogData
{
    public Guid ID { get; set; }
    public string Name { get; set; }
}

我使用 Entity Framework Core 将这些模型保存到 SQLite 数据库中,效果很好。

我需要从数据中删除(它是动态的,我不能使用对象),所以我使用以下命令:

string command="DELETE FROM LogData WHERE ID IN ('ea53b72a-4ab2-4f88-8f1d-0f96baa7cac7')";
context.Database.ExecuteSQLCommand(command);

根据SQLitesyntax,是有效的。

不幸的是,结果我得到了 0,所以没有行受到影响。 当我删除 WHERE 条件时,它会删除表格的内容

我猜测由于键列是Guid,并且存储为BLOB,所以普通的SQLite引擎找不到它。

所以我尝试将命令更改为:

string command="DELETE FROM LogData WHERE HEX(ID) IN ('ea53b72a-4ab2-4f88-8f1d-0f96baa7cac7')";
context.Database.ExecuteSqlCommand(command);

也试过这个:

string command="DELETE FROM AuditLog WHERE HEX(ID) = 'ea53b72a-4ab2-4f88-8f1d-0f96baa7cac7'";
context.Database.ExecuteSqlCommand(command);

这也是:

string command="DELETE FROM AuditLog WHERE ID = 'ea53b72a-4ab2-4f88-8f1d-0f96baa7cac7'";
context.Database.ExecuteSqlCommand(command);

这些都没有帮助。

我该怎么办?

【问题讨论】:

  • 首先,为什么ID存储为BLOB?您可以将其切换为 UNIQUEIDENTIFIER 吗?还有数据保存后是什么样子的?你可能会遇到这里描述的这个问题neosmart.net/blog/2018/…
  • @DavidG 如果我使用它,我会得到一个例外:SQLite Error 1: 'unrecognized token: "X'ea53b72a-4ab2-4f88-8f1d-0f96baa7cac7'"
  • context.SaveChanges(); ?
  • @DavidG 仍然无法正常工作:(
  • @Mihai 由于脚本在没有WHERE 条件的情况下工作:是的,它被持久化到数据库中。

标签: c# sqlite entity-framework-core


【解决方案1】:

GUID 以二进制 BLOB 的形式存储在数据库中,这意味着您需要传入二进制值以进行比较。为此,您使用X'...' 表示法。此外,您需要将 GUID 的endianness 转换为小端。幸运的是,有一个方便的扩展方法here 可以进行转换:

public static Guid FlipEndian(this Guid guid)
{
    var newBytes = new byte[16];
    var oldBytes = guid.ToByteArray();

    for (var i = 8; i < 16; i++)
        newBytes[i] = oldBytes[i];

    newBytes[3] = oldBytes[0];
    newBytes[2] = oldBytes[1];
    newBytes[1] = oldBytes[2];
    newBytes[0] = oldBytes[3];
    newBytes[5] = oldBytes[4];
    newBytes[4] = oldBytes[5];
    newBytes[6] = oldBytes[7];
    newBytes[7] = oldBytes[6];

    return new Guid(newBytes);
}

你可以这样使用它:

//The source GUID
var source = Guid.Parse("ea53b72a-4ab2-4f88-8f1d-0f96baa7cac7");
//Flip the endianness
var flippedGuid = source.FlipEndian();

//Create the SQL
var command = $"DELETE FROM AuditLog WHERE ID = X'{flippedGuid.ToString().Replace("-", "")}'";

context.Database.ExecuteSqlCommand(command);

【讨论】:

  • @Thanks,它确实有效:) 我不知道,也永远找不到。我接受它作为答案。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2019-03-12
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
相关资源
最近更新 更多