c# - EFCore 2.2 找不到 CLR 类型 'Expression[]' 到关系类型的映射

标签 c# .net linq entity-framework-core-2.2

我有一个项目曾经使用 DBML 作为 ORM 从我们的数据库中获取数据。我们正在用 Entity Framework Core 2.2.6 替换此 DBML(遗憾的是我们可以使用的最新版本,因为我们必须支持旧的 .Net 4 框架代码)。

我有一个 LINQ block ,它使用旧的 DBML 代码工作,但在 EF Core 框架 2.2.6 中不起作用。 我收到的错误消息是:

No mapping to a relational type can be found for the CLR type 'Expression[]'.

  List<datadictproperty> dprop = (from dd in dBConnection.datadictproperties
                                  where
                                    dd.entityid == ent.id &&
                                    dd.clientid == clientid &&
                                    ((((dd.suppress <= emp.seclevel &&
                                        dd.suppress != Constants.g_everyone_seclevel) ||
                                       dd.suppress == null)) || ignoreSecLevel) ||
                                    (!
                                      (from d in dBConnection.datadictproperties
                                       where
                                       d.entityid == ent.id &&
                                       d.clientid == clientid &&
                                       d.name == dd.name
                                       select new
                                       {
                                         d.name
                                       }).Contains(new { dd.name }) &&
                                     dd.entityid == ent.id &&
                                     dd.clientid == null &&
                                     ((((dd.suppress <= emp.seclevel &&
                                         dd.suppress != Constants.g_everyone_seclevel) ||
                                        dd.suppress == null)) || ignoreSecLevel))
                                  orderby
                                    dd.name
                                  select dd).ToList();

我可以添加的一件事可能会使这更容易,因为以下语句应返回零行或一行,因此 FirstOrDefault() 可能有效。目前它正在返回匿名 List<T>类型。

(from d in dBConnection.datadictproperties where
    d.entityid == ent.id &&
    d.clientid == clientid &&
    d.name == dd.name
        select new
        {
          d.name
        })

我认为不成立的声明如下:

d.name == dd.name
select new
{
  d.name
}).Contains(new { dd.name })

我的问题是我不知道如何更改此代码以使用 EF Core 2.2.6 技术正确获取相同的数据。

关于如何更改此代码以生成相同数据从而不产生上述运行时错误,是否有人可以给我一些想法?

任何帮助将不胜感激:)

最佳答案

我已经想出如何修复这段代码。最终结果是使用以下代码:

List<datadictproperty> dprop = dBConnection.datadictproperties.Where(
                                    dd =>
                                      dd.entityid == ent.id &&
                                      dd.clientid == clientid &&
                                      ((((dd.suppress <= emp.seclevel &&
                                            dd.suppress != Constants.g_everyone_seclevel) ||
                                            dd.suppress == null)) || ignoreSecLevel) ||
                                        (!(dBConnection.datadictproperties.Where(d =>
                                            d.entityid == ent.id &&
                                            d.clientid == clientid &&
                                            d.name == dd.name).Select(d => d.name)).Contains(dd.name) &&
                                            dd.entityid == ent.id &&
                                            dd.clientid == null &&
                                            (((dd.suppress <= emp.seclevel &&
                                                dd.suppress != Constants.g_everyone_seclevel) ||
                                            dd.suppress == null) || ignoreSecLevel))
                                    ).OrderBy(dd => dd.name).ToList();

将其更改为以下内容似乎有效。我假设是因为一旦选择它就不是匿名类型。

(dBConnection.datadictproperties.Where(d =>
                                            d.entityid == ent.id &&
                                            d.clientid == clientid &&
                                            d.name == dd.name).Select(d => d.name))

关于c# - EFCore 2.2 找不到 CLR 类型 'Expression[]' 到关系类型的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60442950/

相关文章:

c# - LINQ 列表 : Specified cast is not valid

c# - Windows Phone 8.1 图像异步不更新

c# - 彻底检查电子邮件是否存在,NHibernate,LINQ

C# Unity onClick 事件中函数的参数错误

c# - Environment.TickCount 与 DateTime.Now

.net - VB.Net 从任务管理器中隐藏进程

c# - Linux 上的 .NET Core 命名管道问题(地址已被使用)

c# - 从 xml 中检索数据(格式错误?)c#

c# - 有没有一种简单的方法可以替换 Azure 云服务引用 dll,而无需重新发布服务?

c# - <> 是什么意思?