简体   繁体   中英

System.Linq.Expressions exception thrown when using FirstOrDefault in .Net Core 2.1

I am receiving ~300+ exceptions that are spammed in my server output labeled:

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

The query I am using is as follows:

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);

Eventually the exceptions stop generating, it shows a large query in the output window, and everything continues as normal.

If I change the query to the following I do not experience the exception:

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);

However, if I call anything such as First , FirstOrDefault , Single , etc on the IQueryable<Account> variable the exceptions start up again and then stop after ~300.

These exceptions are stalling user logins upwards of 30 seconds or more. The duration of exceptions grows with the amount of data being returned from the database.

I use the Account object by passing it around the server to perform varying maintenance tasks on it and then eventually sending the object client-side where I have it being deserialized into the client-side version of the Account class.

Does anyone know what could be causing these internal exceptions and how I might be able to eliminate or mitigate them?

Here is my output log: 在此处输入图片说明

Below is the exception message: The AccountStatistics isn't listed in the query above because there are about 20 some includes and I shorthanded the include list for brevity.

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]'

There is no inner exception. I double checked my database and I have an entry for the user and all of their fields are filled with valid data.

Account Class (Edited for brevity)

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; }
    }

Account Statistics class

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

Edit

Keys for the Account Statistics table

在此处输入图片说明

   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);
            });

Edit 9001

After doing some testing I've realized the exception only occurs when chaining includes.

This will cause an exception:

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);

This will NOT cause an exception:

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

It does not matter if its currency and unlock, friends and currency, settings, and statistics. Any combination of includes (2 or more) causes the exception to happen.

Edit 9002

Here are my results of the following query:

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

Exception:

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]''

I feel like this is treating AccountSettings as a collection when it's a single field reference.

Edit Final: I never found a fix for this issue. I re-created all the tables and such in another environment and it works just fine. Not a very ideal solution to blow away all tables, classes, and migrations, but it's the only thing that fixed the issue.

I was getting this issue when debugging whilst my colleagues weren't. After much head scratching we discovered I was the only one with the Debugging > General > Enable Just My Code setting unticked.

Ticking it hide the thousands of Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll errors in the Output window, the code went back to normal speed and I could happily live on with my head buried in the sand.

I have the same problem. It's a bug related to Debugging (see https://github.com/aspnet/EntityFrameworkCore/issues/12548 ) and will only be fixed in version 3.0.

Just wanted to add my 2 cents here.

It appears we have to live with this until 3.0 but, disabling all exceptions from other code is not a good idea. You may miss something important during debugging.

I'm on .NET Core 2.2.7 but, this still comes up from time to time.

Using Visual Studio 2019 (I assume 2017 too) you can disable this exception just for System.Linq.Expressions.

The way to do it is as follows :

  • Open Exception Settings window. Debug -> Windows -> Exception Settings (Ctrl+Alt+E)
  • There is a search box at the top right of the window, type "System.ArgumentException" in it
  • You will see the exception listed in the window, right click on it and select "Edit Conditions" from the menu
  • Edit Conditions allows you to set the conditions for the debugger to break when the exception is thrown
  • From left to right, select Module Name, select Not Equals and type "System.Linq.Expressions.dll" in the edit box at the right
  • Click OK and close the window

The same problem will not bother you again, but you will be able to catch the same exception from other places. I find this quite helpful during debugging.

Cheers.

@Sean Malone's answer did the trick for me. Checking the option "Enable Just My Code" in Visual Studio, Menu Tools => Options => Debugging => General

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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