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

Entities for projection collection are tracked, but collection remains empty #16817

Closed
mnissl opened this issue Jul 29, 2019 · 3 comments
Closed

Comments

@mnissl
Copy link

mnissl commented Jul 29, 2019

This issue is basically a follow up of #16636.

var entities = context.Products.Select(p => new ProductWrapper(p)
  {
    UserName = p.UserNavigation.Name,
    Components = new MyCustomList<ComponentWrapper>(
      p.Components.Select(c => new ComponentWrapper(c)
      {
        UserName = c.UserNavigation.Name
      }))
  });

While EF Core 3 Preview 7 (opposed to EF Core 2.2) does track all related entities (context.ChangeTracker.Entries().Count() returns the correct number), the Components collection remains empty!

Using ToList() instead of newing up manually a collection works in turn:

var entities = context.Products.Select(p => new ProductWrapper(p)
  {
    UserName = p.UserNavigation.Name,
    Components = p.Components.Select(c => new ComponentWrapper(c)
      {
        UserName = c.UserNavigation.Name
      }).ToList()
  });

But I need to use MyCustomList<T> instead of List<T>.

Further technical details

EF Core version: 3.0.0-preview7.19362.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 1903
IDE: Visual Studio 2019 16.2.0

@ajcvickers
Copy link
Contributor

@smitpatel @maumar What is the expected behavior here when using a custom collection constructor?

@mnissl
Copy link
Author

mnissl commented Jul 30, 2019

@smitpatel @maumar What is the expected behavior here when using a custom collection constructor?

I'd like to point out that MyCustomList<T> is not the issue; it also doesn't work with new List<T>():

var entities = context.Products.Select(p => new ProductWrapper(p)
  {
    UserName = p.UserNavigation.Name,
    Components = new List<ComponentWrapper>(
      p.Components.Select(c => new ComponentWrapper(c)
      {
        UserName = c.UserNavigation.Name
      }))
  });

Combining MyCustomList<T> with ToList() doesn't work either:

var entities = context.Products.Select(p => new ProductWrapper(p)
  {
    UserName = p.UserNavigation.Name,
    Components = new MyCustomList<ComponentWrapper>(
      p.Components.Select(c => new ComponentWrapper(c)
      {
        UserName = c.UserNavigation.Name
      }).ToList())
  });

@smitpatel
Copy link
Contributor

Duplicate of #16318

When we invoke new List<>(...) the passed in parameter is empty list, which we populate afterwards. Hence the created list (custom or not) remains empty.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants