Repository navigation
Description
Hello. I am using EntityFrameworkCore 1.0.0-rc2-final. I have my entity classes generated by Scaffold-DbContext command from my MS SQL database:
class tRealty
{
int ID;
string caption;
....
}
public class tRealty_History
{
int price;
int? realtyID; // FK to tRealty
virtual tRealty realty { get; set; }
}
public class Context : DbContext
{
...
modelBuilder.Entity<tRealty_History>(entity =>
{
entity.HasOne(d => d.realty)
.WithMany(p => p.tRealty_History)
.HasForeignKey(d => d.realtyID)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK_tRealty_History_tRealty");
}
......
}
The issue
When I want select some tRealty objects I use:
var result = this.Context.tRealty.Where(x => x.ID == 123).Select(realty => new
{
caption = realty.caption,
priceHistory = realty.tRealty_History.Select(x => x.price)
}).FirstOrDefault();
This query takes very long time because it generates this SQL:
SELECT TOP(1) [x].[caption], [x].[ID]
FROM [tRealty] AS [x]
WHERE [x].[ID] = @__id_0
SELECT [x0].[realtyID], [x0].[priceNum]
FROM [tRealty_History] AS [x0]
So it selects entire table! No inner join here. It takes a lot of time to return just one record (It downloads all rows from table and then it does select result in memory?)
When I write inner join by linq query:
var result = this.Context.tRealty.Where(x => x.ID == 123).Select(realty => new
{
caption = realty.caption,
// manually inner join
priceHistory = from history in p.tRealty_History
join property in this.Context.tRealty on history.realtyID equals property.ID
where history.realtyID == id
select history.priceNum
}).FirstOrDefault();
Then it generete quick SQL with inner join:
......
SELECT [history].[realtyID], [history].[priceNum]
FROM [tRealty_History] AS [history]
INNER JOIN [tRealty] AS [property] ON [history].[realtyID] = [property].[ID]
WHERE [history].[realtyID] = @__id_1
So my question is why I must write manually inner joins, why EF doesn't recognize it automatically when I have this.Context.tRealty.Select(x => x.tRealty_History); ? Am i missing something?
Thank you.
Further technical details
EF Core version: 1.0.0-rc2-final
Operating system: WIN10
Visual Studio version: VS2015 update 2
Activity
rowanmiller commentedon Jun 16, 2016
@anpete @maumar could you help me find the dupe of this one. It's related to #4007 but I think we had a couple of issues that were more specifically about filters not being applied to nav prop sub queries in a projection.
divega commentedon Jun 16, 2016
@rowanmiller are you looking for #5754 and #5738 that you mentioned in the first comment of #4007? I think those are about the lack of filtering and you can add a reference to this one too.
rowanmiller commentedon Jun 20, 2016
Thanks, closing as a dupe
rowanmiller commentedon Jun 21, 2016
BTW we split this unfiltered issue off from #4007 - it is now tracked by #5738