-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Query: ArgumentException when chaining projections (best guess) #6938
Comments
When I remove the chained projections, I get a new error:
Exception
Stack trace:
One inner exception:
Stack trace:
|
If I cut down my original query to the point where it runs, any collections of Intellisense (and the compiler) agree there's a coercion operator, but when I again try to cast my property projections via Happy to post more details as necessary - if there' a workaround in the meantime I'd be very grateful. Maybe it's just my model, but I'm really struggling to wrangle EF to do anything this week! |
When trying to reproduce, consider using this more "complete" model: #7102 |
We now throw different exception (dangling qsre) Simplified repro: class Program
{
static void Main(string[] args)
{
using (var ctx = new MyContext())
{
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
var query = ctx.WidgetImages
.Select(i => new
{
usages = i.UsedWidgets.Select(ub => ub.WidgetUsage)
.Where(bu => bu.Retired)
})
.Select(i => new
{
bonds = i.usages.Select(u => u.Bond.Id).Distinct(),
joints = i.usages.Select(u => u.Joint.Id).Distinct(),
});
var result = query.ToList();
}
}
}
public class MyContext : DbContext
{
public DbSet<WidgetImage> WidgetImages { get; set; }
public DbSet<ImageWidgetUsageLink> ImageWidgetUsageLinks { get; set; }
public DbSet<WidgetUsage> WidgetUsages { get; set; }
public DbSet<Bond> Bonds { get; set; }
public DbSet<Joint> Joints { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=.;Database=Repro6938;Trusted_Connection=True;MultipleActiveResultSets=True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ImageWidgetUsageLink>().HasKey(l => new { l.ImageId, l.WidgetUsageId });
modelBuilder.Entity<ImageWidgetUsageLink>().HasOne(l => l.Image).WithMany(i => i.UsedWidgets).IsRequired(true).HasForeignKey(l => l.ImageId);
modelBuilder.Entity<ImageWidgetUsageLink>().HasOne(l => l.WidgetUsage).WithMany(u => u.ImageLinks).IsRequired(true).HasForeignKey(l => l.WidgetUsageId);
}
}
public class WidgetImage
{
public Guid Id { get; set; }
public List<ImageWidgetUsageLink> UsedWidgets { get; set; }
}
public class ImageWidgetUsageLink
{
public Guid ImageId { get; set; }
public WidgetImage Image { get; set; }
public Guid WidgetUsageId { get; set; }
public WidgetUsage WidgetUsage { get; set; }
}
public class WidgetUsage
{
public Guid Id { get; set; }
public bool Retired { get; set; }
public Bond Bond { get; set; }
public Joint Joint { get; set; }
public List<ImageWidgetUsageLink> ImageLinks { get; set; }
}
public class Bond
{
public Guid Id { get; set; }
}
public class Joint
{
public Guid Id { get; set; }
} query plan:
|
This works in 3.1 release. Generated SQL SELECT [w].[Id], [t].[Id], [t].[ImageId], [t].[WidgetUsageId], [t].[Id0], [t0].[Id], [t0].[ImageId], [t0].[WidgetUsageId], [t0].[Id0]
FROM [WidgetImages] AS [w]
LEFT JOIN (
SELECT DISTINCT [b].[Id], [i].[ImageId], [i].[WidgetUsageId], [w0].[Id] AS [Id0]
FROM [ImageWidgetUsageLinks] AS [i]
INNER JOIN [WidgetUsages] AS [w0] ON [i].[WidgetUsageId] = [w0].[Id]
LEFT JOIN [Bonds] AS [b] ON [w0].[BondId] = [b].[Id]
WHERE [w0].[Retired] = CAST(1 AS bit)
) AS [t] ON [w].[Id] = [t].[ImageId]
LEFT JOIN (
SELECT DISTINCT [j].[Id], [i0].[ImageId], [i0].[WidgetUsageId], [w1].[Id] AS [Id0]
FROM [ImageWidgetUsageLinks] AS [i0]
INNER JOIN [WidgetUsages] AS [w1] ON [i0].[WidgetUsageId] = [w1].[Id]
LEFT JOIN [Joints] AS [j] ON [w1].[JointId] = [j].[Id]
WHERE [w1].[Retired] = CAST(1 AS bit)
) AS [t0] ON [w].[Id] = [t0].[ImageId]
ORDER BY [w].[Id], [t].[ImageId], [t].[WidgetUsageId], [t].[Id0], [t0].[ImageId], [t0].[WidgetUsageId], [t0].[Id0] Single query fixed this. |
Steps to reproduce
Trying to work around the other issue I just raised by grabbing the whole DB model into memory and manually querying. New internal exception with that one too unfortunately.
Same model as #6937 .
I'm chaining selects in order to define a subexpression (
usages
in the first projection) to avoid repeating it in the second.The issue
Exception:
Stack trace:
Further technical details
EF Core version:
1.1.0-preview1-*
Operating system: Windows 10
Visual Studio version: n/a (
dotnet
, 1.1 Preview 1)The text was updated successfully, but these errors were encountered: