Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Uncorrelated expressions (without any database reference) are projected out as the same instance for all returned results in client eval #36864

@mtren

Description

@mtren

Bug description

If there are two List Creations in a projection and in one of them there are only static values. The results share the same List.
But each Result should have its own list.

Your code

[TestMethod]
    public async Task NoStaticListTest()
    {
        var ctx = new NoStaticListDbContext();
        ctx.Database.EnsureCreated();
        ctx.Es.Add(new E() { Id = "1", City = "A" });
        ctx.Es.Add(new E() { Id = "2", City = "B" });
        await ctx.SaveChangesAsync();
        ctx.ChangeTracker.Clear();

        var result = await ctx.Es.Select(_ => new EDto { Id = _.Id, Sources = new List<string> { "NoStaticListDbContext" }, Addresses = new List<Address> { new Address { City = _.City } } }).ToListAsync();
        result[0].Sources.Add("b");
        result[1].Sources.Should().HaveCount(1);
    }

    public class NoStaticListDbContext : DbContext
    {
        public DbSet<E> Es { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=localhost;Database=One;Integrated Security=True;Trust Server Certificate=True");
        }

    }
    public class E
    {
        public string Id { get; set; }
        //public string Zip { get; set; }
        public string City { get; set; }
        //public string Street { get; set; }
        //public string Number { get; set; }
    }
    public class EDto
    {
        public string Id { get; set; }
        public List<string> Sources { get; set; }
        public List<Address> Addresses { get; set; }
    }
    public class Address
    {
        public string City { get; set; }
    }

Stack traces


Verbose output


EF Core version

9.0.9

Database provider

No response

Target framework

.NET 8.0

Operating system

No response

IDE

No response

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions