dotnet / dotnet/efcore

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

Open
#36,864 2 comments 0 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### 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

```csharp
[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 { "NoStaticListDbContext" }, Addresses = new List

{ new Address { City = _.City } } }).ToListAsync();
result[0].Sources.Add("b");
result[1].Sources.Should().HaveCount(1);
}

public class NoStaticListDbContext : DbContext
{
public DbSet 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 Sources { get; set; }
public List

Addresses { get; set; }
}
public class Address
{
public string City { get; set; }
}
```

### Stack traces

```text

```

### Verbose output

```text

```

### EF Core version

9.0.9

### Database provider

_No response_

### Target framework

.NET 8.0

### Operating system

_No response_

### IDE

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.