简体   繁体   English

Entity Framework Core ExecuteSqlCommand 删除 SQLite 不起作用

[英]Entity Framework Core ExecuteSqlCommand delete with SQLite does not work

I have the following model:我有以下模型:

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

I use Entity Framework Core to save those models to an SQLite database, it works well.我使用Entity Framework Core将这些模型保存到SQLite数据库中,效果很好。

I need to delete from the data (it is dynamic, I can't use objects), so I use the following command:我需要从数据中删除(它是动态的,我不能使用对象),所以我使用以下命令:

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

According to the SQLite syntax , it is valid.根据 SQLite语法,它是有效的。

Unfortunately, as a result I get back 0, so no row was affected.不幸的是,结果我得到了 0,所以没有行受到影响。 When I remove the WHERE condition, it deletes the contents of the table .当我删除WHERE条件时,它会删除 table 的内容

I have a guessing that as the key column is a Guid and it is stored as a BLOB , the plain SQLite engine can't find it.我有一个猜测,因为键列是一个Guid并且它存储为一个BLOB ,普通的 SQLite 引擎找不到它。

So I tried to alter the command to this:所以我尝试将命令更改为:

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

Also tried this:也试过这个:

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

This, too:这个也是:

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

None of those helped.这些都没有帮助。

What should I do about this?我该怎么办?

The GUIDs are stored in the database as a binary BLOB meaning you need to pass in a binary value to compare against. GUID 作为二进制BLOB存储在数据库中,这意味着您需要传入一个二进制值进行比较。 To do this you use the X'...' notation.为此,您可以使用X'...'符号。 Additionally, you need to convert the endianness of the GUID to little endian.此外,您需要将 GUID 的字节序转换为小字节序。 Fortunately, there's a handy extension method here to do your conversion:幸运的是,有一个方便的扩展方法在这里做你的转换:

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

And you use it like this:你像这样使用它:

//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);

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

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