-
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
EF 7 LINQ query with .Select() including collection throws ArgumentNullException when .Take() is used #3804
Comments
Appears to be the same code as #3807 but one reports bad SQL and the other an exception |
Yes, the code is almost the same. Basically, if you have a Take(), it fails with an exception. If you comment that out, it works, however, the generated SQL looks wrong. It works, but, it would be very inefficient if the table had a large number or rows since it isn’t filtering and is querying for the entire table (the subqueries). |
Ok I see now that your comments mention that commenting out the Take makes it work. We were just skimming you repro code and the Take is commented out - so we assumed you saw the error with the code listing provided. I updated the listing to be the code that reproduces the issue. |
OK, thanks. |
I found that calling .Take() before .Select() throws a TargetInvocationException, while calling Take after Select works seemingly correctly. The ordering of these wasn't a problem in EF 6. I don't know why my exception is different from jemiller0's because it seems like I encountered the exact same problem. I'm also on EF 7 RC 1. |
Hello, I am working on large scale project with my team and we have started using EF7. We have encountered the same bug as creeve TargetInvocationException. I was wondering is there any quick fix or should we go back to EF6? |
Just found some workaround. First use select, then .AsQuerayable().Skip().Take().ToList(). That should do the trick. Works for me. |
Same problem here (latest RC1 bits), @robertledinski workaround indeed does the trick. Note that the issue can also be reproduced when using simple navigation properties. My reproduction: public class Foo {
public int Id { get; set; }
public Bar Bar { get; set; }
}
public class Bar {
public int Id { get; set; }
public string Test { get; set; }
}
public class AppContext : DbContext {
public DbSet<Foo> Foo { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder o) => o.UseSqlServer("...");
}
void Test()
{
using (var c = new AppContext())
{
var r = c.Foo
.Take(1) // this will cause an exception
.Skip(1) // this will also cause an exception
.Select(x => x.Bar.Test) // when selecting a property of a relation that is not part of the foreign key
//.Select(x => x.Bar.Id) // but not if selecting the primary key instead
.ToList();
}
} exception stacktrace: |
Verified that this issue reproduces in |
I found that the query below throws an ArgumentNullException when used with EF 7 RC 1. It works in EF 6. If you comment out the .Take(100), the query works. Also, it doesn't matter if there are any rows in the database. Initially, I was using .FirstOrDefault() which has the same problem, but, I found that the problem even occurs if you use .Any().
The text was updated successfully, but these errors were encountered: