简体   繁体   English

如何使用 Entity Framework 和 .NET 5.0 构建查询以在相关表中查找不匹配的实体?

[英]How do I contruct a query to find unmatched entities in related table with Entity Framework and .NET 5.0?

I am using .NET (Core?) 5.0 and Entity Framework with a MySQL 8.0 database using the Pomelo driver.我正在使用 .NET (Core?) 5.0 和 Entity Framework 以及使用 Pomelo 驱动程序的 MySQL 8.0 数据库。 In a previous version of .NET Core 3.1 and MySQL 5.7 I was able to create a query or lambda expression to find all records in one table that DO NOT have a corresponding record in another related table for a given user.在 .NET Core 3.1 和 MySQL 5.7 的先前版本中,我能够创建查询或 lambda 表达式来查找一个表中的所有记录,这些记录在给定用户的另一个相关表中没有相应记录。 Here is the method as it worked before:这是以前工作的方法:

public int GetUnacknowledgedAlarmEventsCount(string name, ApplicationUser user)
{
    int count = 0;
    if (name != null && user != null)
    {
        count = (from ae in AlarmEvents
                 where ae.Alarm.Name.StartsWith(name) && !ae.Acks.Any(a => a.UserId == user.Id)
                 select ae).Count();
    }
    return count;
}

The structure of the tables behind this query are simplified as:此查询背后的表结构简化为:

Alarms
------
Id
Name

AlarmEvents
-----------
Id
AlarmId

AlarmEventAcks
--------------
Id
AlarmEventId
UserId

Users
-----
Id

I hope the relationships are obvious based on the names.我希望基于名称的关系是显而易见的。 Anyway, I am trying to determine which alarms events have NOT been acknowledged by a given user.无论如何,我正在尝试确定给定用户尚未确认哪些警报事件。 I know I can do this in multiple steps with a huge performance hit, but I am hoping to put all the work on the database query just like it worked in a previous version.我知道我可以在多个步骤中执行此操作,并且性能会受到很大影响,但我希望将所有工作都放在数据库查询上,就像在以前的版本中一样。 Not sure why it does not work the same as before other than the lazy loading not kicking in like previous versions did by default.不知道为什么它不像以前那样工作,除了延迟加载不像以前的版本默认那样启动。

I tried changing this query to use Lambda expressions with Includes like below, but that didn't work either:我尝试更改此查询以使用包含如下所示的 Lambda 表达式,但这也不起作用:

public int GetUnacknowledgedAlarmEventsCount(string station, ApplicationUser user)
{
    int count = 0;
    if (station != null && user != null)
    {
        count = AlarmEvents.Include(a => a.Alarm).Include(a => a.Acks).Where(a => a.Alarm.Name.StartsWith(station)).Where(a => !a.Acks.Any(a => a.UserId == user.Id)).Count();
    }
    return count;
}

So, I am stuck trying to figure out the best way to construct this query to work with the changes to Entity Framework for the newest version of .NET.因此,我一直在试图找出构建此查询的最佳方法,以处理最新版本的 .NET 对实体框架的更改。 Anyone have any suggestions or help?任何人有任何建议或帮助? Thanks.谢谢。

After updating everything to make sure everything was in sync with current versions of drivers, it is now working again using the original queries.在更新所有内容以确保所有内容与当前版本的驱动程序同步后,它现在可以使用原始查询再次运行。 I hate upgrading stable, working code to use newer versions of the technologies and APIs.我讨厌升级稳定的工作代码以使用更新版本的技术和 API。

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

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