在.Net Core 2.1中使用FirstOrDefault时引发System.Linq.Expressions异常

时间:2018-08-28 20:56:05

标签: c# sql-server entity-framework

我收到了约300多个在我的服务器输出中标记为垃圾邮件的异常:

std::set

我正在使用的查询如下:

Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll

最终异常停止生成,它在输出窗口中显示一个大查询,并且一切正常进行。

如果我将查询更改为以下内容,则不会遇到异常:

Account account = _accountContext.Account.
     Include(i => i.Currency).
     Include(i => i.Unlocks).
     Include(i => i.Settings).
     Include(i => i.Friends).
     FirstOrDefault(a => a.FacebookUserID == facebookUserID);

但是,如果我在IQueryable<Account> account = _accountContext.Account. Include(i => i.Currency). Include(i => i.Unlocks). Include(i => i.Settings). Include(i => i.Friends). Where(a => a.FacebookUserID == facebookUserID); 变量上调用FirstFirstOrDefaultSingle等,则异常会再次启动,然后在〜300之后停止。

这些例外使用户登录停滞了30秒或更长时间。异常的持续时间随着从数据库返回的数据量的增加而增加。

我使用Account对象,方法是将其传递到服务器周围以执行各种维护任务,然后最终将其反序列化的对象客户端发送到Account类的客户端版本中。

有人知道什么可能导致这些内部异常,以及我如何能够消除或缓解这些异常?

这是我的输出日志: enter image description here

以下是异常消息: 上面的查询中未列出IQueryable<Account>,因为大约有20个包含,为简洁起见,我简化了包含列表。

AccountStatistics

没有内部异常。 我仔细检查了数据库,并为用户输入了一个条目,他们的所有字段都填充了有效数据。

帐户类别(为简便起见而编辑)

Field 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Project.Models.Account,System.Collections.Generic.IEnumerable`1[Project.Models.AccountStatistics]].Inner' is not defined for type 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Project.Models.Account,Project.Models.AccountStatistics]'

帐户统计信息类

public class Account
    {
        [Key]
        public int ID { get; set; }
        public DateTime CreationDate { get; set; }

        public AccountCurrency Currency { get; set; }
        public AccountProgression Progression { get; set; }
        public AccountSettings Settings { get; set; }
        public AccountStatistics Statistics { get; set; }

        public ICollection<AccountFriendEntry> Friends { get; set; }
        public ICollection<AccountUnlockedGameEntry> Unlocks{ get; set; }
    }

修改

“帐户统计信息”表的键

enter image description here

public class AccountStatistics
{
    [Key]
    public int AccountID { get; set; }
    public int LoginCount { get; set; }
    public DateTime LastLoginTime { get; set; }
    public DateTime LastActivityTime { get; set; }
}

编辑9001

做完一些测试后,我意识到只有链接包括时才会发生异常。

这将导致异常:

   migrationBuilder.CreateTable(
            name: "AccountStatistics",
            columns: table => new
            {
                AccountID = table.Column<int>(nullable: false),
                LoginCount = table.Column<int>(nullable: false),
                LastLoginTime = table.Column<DateTime>(nullable: false),
                CreationDate = table.Column<DateTime>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AccountStatistics", x => x.AccountID);
                table.ForeignKey(
                    name: "FK_AccountStatistics_Accounts_AccountID",
                    column: x => x.AccountID,
                    principalTable: "Accounts",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Cascade);
            });

这不会引起异常:

Account account = _accountContext.Account.
     Include(i => i.Currency).
     Include(i => i.Unlocks).
     Include(i => i.Settings).
     Include(i => i.Friends).
     FirstOrDefault(a => a.FacebookUserID == facebookUserID);

它的币种和解锁,朋友和币种,设置和统计信息都没有关系。包含(2个或更多)的任何组合都会导致异常发生。

编辑9002

这是我的以下查询结果:

Account account = _accountContext.Account.
     Include(i => i.Currency).
     FirstOrDefault(a => a.FacebookUserID == facebookUserID);

例外:

var acct = _accountContext.Account
     .Where(a => a.FacebookUserID == facebookUserID)
     .Select(x => new { Account = x, x.Currency, x.Settings }).ToList();

我觉得这是将System.ArgumentException: 'Field 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Project.Models.Account,System.Collections.Generic.IEnumerable`1[Project.Models.AccountSettings]].Inner' is not defined for type 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Project.Models.Account,Project.Models.AccountSettings]'' 当作一个字段引用时的一个集合。

最终编辑: 我从未找到解决此问题的方法。我在另一个环境中重新创建了所有表等,并且效果很好。删除所有表,类和迁移的方法不是一个理想的解决方案,但这是解决此问题的唯一方法。

4 个答案:

答案 0 :(得分:8)

我的同事在调试时遇到了这个问题。经过一番摸索之后,我们发现我是唯一未勾选 Debugging> General> Enable Just My Code 设置的人。

打勾,将其隐藏在“输出”窗口中,成千上万个Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll错误使代码恢复到正常速度,我可以将头埋在沙子中,继续生活下去。

答案 1 :(得分:2)

我有同样的问题。这是与调试相关的错误(请参见https://github.com/aspnet/EntityFrameworkCore/issues/12548),并且仅在3.0版中已修复。

答案 2 :(得分:2)

@Sean Malone的回答对我有用。 在Visual Studio中,选中菜单工具=>选项=>调试=>常规

中的“启用我的代码”选项

https://docs.microsoft.com/fr-fr/visualstudio/debugger/just-my-code?view=vs-2019

答案 3 :(得分:0)

只想在这里加2美分。

看来我们必须一直使用到3.0,但是禁用其他代码中的所有异常并不是一个好主意。在调试过程中,您可能会错过一些重要的事情。

我使用的是.NET Core 2.2.7,但有时仍然会出现这种情况。

使用Visual Studio 2019(我也假定为2017),可以仅针对System.Linq.Expressions禁用此异常。

方法如下:

  • 打开“例外设置”窗口。调试-> Windows->例外设置(Ctrl + Alt + E)
  • 窗口右上方有一个搜索框,在其中键入“ System.ArgumentException”
  • 您将在窗口中看到例外,右键单击它,然后从菜单中选择“编辑条件”
  • 编辑条件允许您设置引发异常时调试器中断的条件
  • 从左至右,选择“模块名称”,选择“不等于”,然后在右侧的编辑框中键入“ System.Linq.Expressions.dll”
  • 单击“确定”并关闭窗口

相同的问题不会再困扰您,但是您将能够从其他地方捕获相同的异常。我发现这在调试过程中很有帮助。

干杯。