Efcore: System.ArgumentException: 'Field 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier

Created on 1 Mar 2019  路  7Comments  路  Source: dotnet/efcore

```C#

namespace Foo.Technotes.Models {
public abstract class BaseEntity
{
public BaseEntity()
{ }
[Key]
public int Id { get; set; }
}

public class Section : BaseEntity
{
[DataType(DataType.Text)]
public string Title { get; set; }

public ICollection<Category> Categories { get; set; } = new List<Category>();

}

public class Category : BaseEntity
{

public Category ParentCategory { get; set; }
public virtual ICollection<Category> ChildCategories { get; set; } = new List<Category>();
public virtual ICollection<CategoryTranslation> CategoryTranslations { get; set; } = new List<CategoryTranslation>();

public Section Section { get; set; }

}

public class CategoryTranslation : BaseEntity
{

public string LanguageId { get; set; }

[DataType(DataType.Text)]
public string Title { get; set; }

[DataType(DataType.MultilineText)]
public string Description { get; set; }

public int CategoryId { get; set; }
public virtual Category Category { get; set; }

}
}


when I add `.Include(cat => cat.Section)` I get this:

System.ArgumentException: 'Field 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier2[Foo.Technotes.Models.Category,System.Collections.Generic.IAsyncEnumerable1[Foo.Technotes.Models.Category]].Inner' is not defined for type 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Foo.Technotes.Models.Category,Foo.Technotes.Models.Category]''


```C#
    public virtual async Task<Category> GetCategoryAsync(int? catid)
    {
      if (catid == null)
        return null;

      var data = await _CategoryRepository.DbSet
        .Include(cat => cat.Section) // <---- produces the argumentexception
        .Include(cat => cat.ChildCategories)
        .ThenInclude(childcat => childcat.CategoryTranslations)
        .Include(cat => cat.CategoryTranslations)
        .Include(cat => cat.ParentCategory)
        .Where(x => x.Id == catid)
        .FirstOrDefaultAsync();
      return data;
    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
      base.OnModelCreating(builder);

      // seed roles
      builder.Entity<ApplicationRole>().HasData(new ApplicationRole
      {
        Name = CustomRoles.Administrator,
        NormalizedName = CustomRoles.Administrator.ToUpper()
      });

      builder.Entity<Section>().ToTable("Section");
      builder.Entity<Category>().ToTable("Category");
      builder.Entity<CategoryTranslation>().ToTable("CategoryTranslation");

        builder.Entity<Section>()
          .HasMany(s => s.Categories)
          .WithOne(c => c.Section)
          .OnDelete(DeleteBehavior.ClientSetNull); 
   }

Further technical details

Operating system: Windows 10 Pro x64 17763.316
IDE: VS2017 15.9.7

data from csproj

    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.2" />

global.json:

{
  "sdk": {
    "version": "2.2.104"
  }
}
area-query closed-fixed customer-reported punted-for-3.0 type-bug

All 7 comments

@wstaelens It would help facilitate investigation if you could provide full definitions for the entity types being used, together with any code in OnModelCreating.

@ajcvickers updated. Hope this is ok?

@wstaelens Thanks!

~blocked by #15611~

Works in 3.1
Generated SQL

      SELECT [t].[Id], [t].[ParentCategoryId], [t].[SectionId], [t].[Id0], [t].[Title], [t].[Id1], [t].[ParentCategoryId0], [t].[SectionId0], [t0].[Id], [t0].[ParentCategoryId], [t0].[SectionId], [t0].[Id0], [t0].[CategoryId], [t0].[Description], [t0].[LanguageId], [t0].[Title], [c3].[Id], [c3].[CategoryId], [c3].[Description], [c3].[LanguageId], [c3].[Title]
      FROM (
          SELECT TOP(1) [c].[Id], [c].[ParentCategoryId], [c].[SectionId], [s].[Id] AS [Id0], [s].[Title], [c0].[Id] AS [Id1], [c0].[ParentCategoryId] AS [ParentCategoryId0], [c0].[SectionId] AS [SectionId0]
          FROM [Category] AS [c]
          LEFT JOIN [Section] AS [s] ON [c].[SectionId] = [s].[Id]
          LEFT JOIN [Category] AS [c0] ON [c].[ParentCategoryId] = [c0].[Id]
          WHERE [c].[Id] = @__catid_0
      ) AS [t]
      LEFT JOIN (
          SELECT [c1].[Id], [c1].[ParentCategoryId], [c1].[SectionId], [c2].[Id] AS [Id0], [c2].[CategoryId], [c2].[Description], [c2].[LanguageId], [c2].[Title]
          FROM [Category] AS [c1]
          LEFT JOIN [CategoryTranslation] AS [c2] ON [c1].[Id] = [c2].[CategoryId]
      ) AS [t0] ON [t].[Id] = [t0].[ParentCategoryId]
      LEFT JOIN [CategoryTranslation] AS [c3] ON [t].[Id] = [c3].[CategoryId]
      ORDER BY [t].[Id], [t0].[Id], [t0].[Id0], [c3].[Id]

I had the same issue. I opened an year old project and got this error, I knew 100% for sure that it worked last time. After closing Visual Studio 2019 and opening the same project in Visual Studio 2017 it was working again. My hunch is that some internals in how the code is generated by the complier and/or runtime has changed and is incompatible with this version of EFCore, causing the Exception.

Was this page helpful?
0 / 5 - 0 ratings