简体   繁体   English

实体框架:删除 c# 语法缺失的 WHERE (sqlite)

[英]entity framework: delete WHERE in c# syntax missing (sqlite)

I am using entity framework to alter data on my remote tables.我正在使用实体框架来更改远程表上的数据。

One working call I do is this:我做的一个工作电话是这样的:

public async Task<List<PictureItems>?> GetUserThumbnailByuserId(string userid)
{
    await InitializeAsync();
    try
    {
        return await _table.GetAsyncItems().Where(x => x.BelongsToUserId == userid).ToListAsync();
    }
    catch (Exception e)
    {
        return null;
    }
}

Now, I am trying to delete an item where Ids match.现在,我正在尝试删除 ID 匹配的项目。

But all I can do to delete the correct item is:但我能做的就是删除正确的项目:

await _table.DeleteItemAsync(item);

But this would only work if I inserted an identical item into the ().但这只有在我将相同的项目插入 () 时才有效。 So I could get the right item, if the item is not null then insert into the delete and the item is deleted.所以我可以获得正确的项目,如果该项目不是 null 则插入到删除中并删除该项目。

But I dont wanna get the item all the times when I have the ID already locally available, this would just create unneccissary traffic.但是当我已经在本地获得 ID 时,我不想一直得到该项目,这只会造成不必要的流量。

So what I would need is something like:所以我需要的是:

_table.DeleteItemAsync().Where(x => x.BelongsToUserId == userid).ToListAsync();

Anyone got any help for me how to approach this?有人对我如何处理这个有任何帮助吗?

Best,最好,

J

If you have knowledge about the primary ID of the items to be deleted beforehand, you can simply instantiate an object only containing that ID and pass it to EF Core:如果您事先知道要删除的项目的主 ID,则可以简单地实例化一个仅包含该 ID 的 object 并将其传递给 EF Core:

table.Remove(new Entity() { Id = <yourId> });

This removes the need to query data first - EF Core will delete the corresponding existing item with the respective ID property if it is already available on the DB.这消除了首先查询数据的需要——如果数据库上已经存在,EF Core 将删除具有相应 ID 属性的相应现有项。

Of course, whether or not this works in your case depends on whether BelongsToUserId is a key EF can work with, or whether it is only a regular property.当然,这是否适用于您的情况取决于 BelongsToUserId 是否是 EF 可以使用的关键,或者它是否只是一个常规属性。

You have several options:您有几种选择:

  • As already mentioned in other answer, if you know the primary keys of the entities that you want to delete, you can construct empty entities with IDs without querying and call DbContext.RemoveRange(entities);正如在其他答案中已经提到的,如果您知道要删除的实体的主键,则可以在不查询的情况下构造具有 ID 的空实体并调用DbContext.RemoveRange(entities);
  • If you're up for trying or using EF Core v.7.0, then you can use The new ExecuteDelete method如果您准备尝试或使用 EF Core v.7.0,则可以使用新的 ExecuteDelete 方法
  • If you know what you're doing and are confident in the quality of your inputs: use ExecuteSqlRaw and just remove the objects you want manually this way: DbContext.Database.ExecuteSqlRaw("DELETE FROM PictureItems WHERE...");如果您知道自己在做什么并且对输入的质量有信心:使用ExecuteSqlRaw并以这种方式手动删除您想要的对象: DbContext.Database.ExecuteSqlRaw("DELETE FROM PictureItems WHERE...");
  • Use a (commercial) extension library that supports bulk-delete on EF Core.使用支持 EF Core 批量删除的(商业)扩展库。 I generally avoid recommending specific tools, but "ef core bulk delete" search in Google gives some relevant results.我通常避免推荐特定工具,但在 Google 中搜索“ef core bulk delete”会给出一些相关结果。 Search for an open-source analogue lead me to this small library that should fit your needs - taken from this answer .搜索一个开源类似物将我带到这个应该满足您需求的小型图书馆- 取自这个答案

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

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