在.Net Core 2.1中使用FirstOrDefault时抛出System.Linq.Expressions异常

ste*_*eve 12 c# sql-server entity-framework

我收到了300多个在我的服务器输出中发送垃圾邮件的例外标记:

Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll
Run Code Online (Sandbox Code Playgroud)

我使用的查询如下:

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);
Run Code Online (Sandbox Code Playgroud)

最终异常停止生成,它在输出窗口中显示一个大型查询,一切都继续正常.

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

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);
Run Code Online (Sandbox Code Playgroud)

但是,如果我叫什么,例如First,FirstOrDefault,Single等的IQueryable<Account>变量异常再次启动再经过〜300停止.

这些例外情况会导致用户登录停顿超过30秒或更长时间.异常的持续时间随着从数据库返回的数据量而增加.

我通过在服务器上传递它来使用Account对象来对其执行不同的维护任务,然后最终将对象客户端发送到我将其反序列化到客户端版本的Account类中.

有谁知道可能导致这些内部异常的原因以及我如何能够消除或减轻它们?

这是我的输出日志: 在此输入图像描述

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

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]'
Run Code Online (Sandbox Code Playgroud)

没有内在的例外.我仔细检查了我的数据库,我有一个用户条目,他们的所有字段都填充了有效数据.

帐户类(为简洁起见编辑)

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; }
    }
Run Code Online (Sandbox Code Playgroud)

帐户统计类

public class AccountStatistics
{
    [Key]
    public int AccountID { get; set; }
    public int LoginCount { get; set; }
    public DateTime LastLoginTime { get; set; }
    public DateTime LastActivityTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

编辑

帐户统计表的键

在此输入图像描述

   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);
            });
Run Code Online (Sandbox Code Playgroud)

编辑9001

在做了一些测试后,我意识到只有当链接包含时才会发生异常.

这将导致异常:

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);
Run Code Online (Sandbox Code Playgroud)

这不会导致异常:

Account account = _accountContext.Account.
     Include(i => i.Currency).
     FirstOrDefault(a => a.FacebookUserID == facebookUserID);
Run Code Online (Sandbox Code Playgroud)

它的货币和解锁,朋友和货币,设置和统计数据无关紧要.包含(2或更多)的任何组合都会导致异常发生.

编辑9002

以下是我对以下查询的结果:

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

例外:

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]''
Run Code Online (Sandbox Code Playgroud)

我觉得这是AccountSettings一个集合,当它是一个单一的字段引用时.

编辑最终: 我从未找到解决此问题的方法.我在另一个环境中重新创建了所有表格,它运行得很好.吹掉所有表,类和迁移并不是一个非常理想的解决方案,但这是解决问题的唯一方法.

小智 14

我在调试时遇到了这个问题而我的同事却没有.经过多次努力之后,我们发现我是唯一一个使用Debugging> General> Enable Just My Code设置未选中的人.

滴答它隐藏了Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll输出窗口中的数千个错误,代码恢复到正常速度,我可以幸福地生活在我的头埋在沙子里.


小智 5

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

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