Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting IEnumerable navigation property with backing field results in ArgumentException #21803

Closed
nphmuller opened this issue Jul 27, 2020 · 0 comments · Fixed by #21881
Closed
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@nphmuller
Copy link

nphmuller commented Jul 27, 2020

I've configured my entity with a navigation property that's of type IEnumerable<T> that has a backing field of type List<T>. When creating a query that Select's this (IEnumerable) navigation property I receive the following exception:

System.ArgumentException: 'GenericArguments[0], 'System.Collections.Generic.IEnumerable`1[ConsoleApp3.OtherEntity]', on 'Void PopulateCollection[TCollection,TElement,TRelatedEntity](Int32, Microsoft.EntityFrameworkCore.Query.QueryContext, System.Data.Common.DbDataReader, Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryResultCoordinator, System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Func`3[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,System.Object[]], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Collections.Generic.IReadOnlyList`1[Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer], System.Func`5[Microsoft.EntityFrameworkCore.Query.QueryContext,System.Data.Common.DbDataReader,Microsoft.EntityFrameworkCore.Query.Internal.ResultContext,Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryResultCoordinator,TRelatedEntity])' violates the constraint of type 'TCollection'.'

Inner Exception:
VerificationException: Method Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+ShaperProcessingExpressionVisitor.PopulateCollection: type argument 'System.Collections.Generic.IEnumerable`1[ConsoleApp3.OtherEntity]' violates the constraint of type parameter 'TCollection'.

Steps to reproduce

Run the following example:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        async static Task Main()
        {
            var dbContext = new AppDbContext();

            await dbContext.Database.EnsureDeletedAsync();
            await dbContext.Database.EnsureCreatedAsync();

            var appEntity = await dbContext.Set<AppEntity>().Select(appEntity => appEntity.OtherEntities).ToListAsync();
        }
    }

    public class AppEntity
    {
        private List<OtherEntity> otherEntities = new List<OtherEntity>();

        public int Id { get; private set; }
        public IEnumerable<OtherEntity> OtherEntities => otherEntities;
    }

    public class OtherEntity
    {
        public int Id { get; private set; }
    }

    public class AppDbContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<AppEntity>();
            modelBuilder.Entity<OtherEntity>();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ConsoleApp3;");
        }
    }
}

csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <!--<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.6" />-->
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0-preview.7.20365.15" />
  </ItemGroup>
</Project>

Further technical details

EF Core version: (via Microsoft.EntityFrameworkCore.SqlServer)
Database provider: Microsoft.EntityFrameworkCore.SqlServer 3.1.6 & 5.0.0-preview.7.20365.15
Target framework: netcoreapp3.1
Operating system: Win 10 (2004)
IDE: VS 16.6.3

@smitpatel smitpatel self-assigned this Jul 31, 2020
@smitpatel smitpatel added type-bug closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Jul 31, 2020
@smitpatel smitpatel modified the milestones: 5.0.0-preview9, 5.0.0-rc1 Jul 31, 2020
smitpatel added a commit that referenced this issue Jul 31, 2020
smitpatel added a commit that referenced this issue Jul 31, 2020
smitpatel added a commit that referenced this issue Aug 1, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc1, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants