-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Error applying projection #30012
Comments
@GLuca74 thanks for the code samples, I can see the errors happening. Here's a minimal repro for the first case, where a single projection works but when it's broken down to two projections it fails: Minimal repro for the first caseawait using var ctx = new Mycontext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
// WORKS:
_ = ctx.Set<Nation>()
.Select(n => n.Cities.First().Districts.Select(d => new { d.DistrictName }))
.ToArray();
// FAILS (same thing broken down to two projections):
var R2 = ctx.Nations
.Select(n => n.Cities.First().Districts)
.Select(districts => districts.Select(d => new { d.DistrictName }))
.ToArray();
public class Mycontext : DbContext
{
public DbSet<Nation> Nations { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Nation
{
public Guid NationID { get; set; }
public string NationName { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
public class City
{
public Guid CityID { get; set; }
public virtual Nation Nation { get; set; }
public string CityName { get; set; }
public virtual ICollection<District> Districts { get; set; }
}
public class District
{
public Guid DistrictID { get; set; }
public string DistrictName { get; set; }
public virtual City City { get; set; }
} For the second case, I can see the error happening with "'ProjectionBindingExpression:0' could not be translated" with 6.0. With 7.0 I instead get the error "The given key 'EmptyProjectionMember' was not present in the dictionary.". Minimal repro for the second caseawait using var ctx = new Mycontext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
var R1 = ctx.Set<City>()
.GroupBy(itm => EF.Property<Guid>(itm, "NationID"))
.Select(itm => itm.First())
.Select(itmSelect => new { itmSelect.CityName })
.ToArray();
public class Mycontext : DbContext
{
public DbSet<Nation> Nations { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Nation
{
public Guid NationID { get; set; }
public string NationName { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
public class City
{
public Guid CityID { get; set; }
public virtual Nation Nation { get; set; }
public string CityName { get; set; }
public virtual ICollection<District> Districts { get; set; }
}
public class District
{
public Guid DistrictID { get; set; }
public string DistrictName { get; set; }
public virtual City City { get; set; }
} |
Note from triage: likely related to pending selector work. |
Hello, we have to decide if we can keep invest time on wath we are doing now(wrote on top), so I am 'forced' to ask you if this will fix for version 6.x. Thank you for understanding |
@GLuca74 This won't get fixed in a 6.0 release. |
Of course I can not know the reasons you decided to not fix this in version 6 but honestly this surprice me. Can you please advide a workaroud? Many thanks |
@GLuca74 we do fix bugs in 6.0, but that doesn't mean that all bugs can be fixed in patch versions. Specifically, the fix here is likely to be quite complex, and therefore bringing with it some considerable risk. See https://learn.microsoft.com/en-us/ef/core/what-is-new/release-planning#patch-releases for more information on what we consider eligible for patching. |
@roji Many thanks for the informations u post, I read the document, can I hope this wil fix in a Minor releases of 6? |
There isn't going to be a minor release of 6. This will likely be fixed only for 8.0. |
Hello,
summarizing, from a third part software I receive an expression that contain a
IQueryable<TEntity>
, I also receive theTEntity
EF Model metadata. This expression may contains any valid query.Under some circumstances, I have to limit the materialized data, so I check the Model metadata, I choice wich properties I want to retrive from database and I add a projection to the query attaching a select call to the expression.
All seems work but in some cases the query fails despite it is a valid query.
for example, having this model
when I receive this query
ctx.Set<Nation>().Select(itm => itm.Cities.First().Districts)
I apply the projection and I get the query
ctx.Set<Nation>().Select(itm => itm.Cities.First().Districts).Select(itm => itm.Select(itmSelect => new { DistrictName = itmSelect.DistrictName })).ToArray();
this fails with
Of course the query fails also if I directly write it in a test project(like the attached)
another expample is this query :
ctx.Set<City>().GroupBy(itm => EF.Property<Guid>(itm, "NationID")).Select(itm => itm.First())
after the projection is added
ctx.Set<City>().GroupBy(itm => EF.Property<Guid>(itm, "NationID")).Select(itm => itm.First()).Select(itmSelect=>new {itmSelect.CityName}).ToArray();
this fail with :
Can you please check where the problem is and advise a workaround?
Many thanks
ConsoleApp1.zip
Provider : Microsoft.EntityFrameworkCore.SqlServer 6.0.12
The text was updated successfully, but these errors were encountered: