-
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
Defining Query with FromSql and Joins Builds Bad SQL #13003
Comments
Update: SELECT [u.Location].[StreetAddress], [u.Brewer].[Description], [u].[Acquired], [u].[OutOfService], [u].[Cost]
FROM (
SELECT Locations.StreetAddress, BrewerTypes.Description as BrewerTypeDescriptionXYZ, Units.Acquired, Units.OutOfService,Units.Cost
FROM Units, Locations, BrewerTypes
WHERE Units.LocationId = Locations.LocationId
AND Units.BrewerTypeId = BrewerTypes.BrewerTypeId
) AS [u]
INNER JOIN [BrewerTypes] AS [u.Brewer] ON [u].[BrewerTypeId] = [u.Brewer].[BrewerTypeId]
LEFT JOIN [Locations] AS [u.Location] ON [u].[LocationId] = [u.Location].[LocationId] Is it another case of DefiningQueries not like navigations ala #11792? |
The issue is this code in defining query definition, Units.FromSql(@" SELECT Locations.StreetAddress, BrewerTypes.Description,
Units.Acquired, Units.OutOfService,Units.Cost
FROM Units
INNER JOIN Locations ON Units.LocationId = Locations.LocationId
INNER JOIN BrewerTypes ON Units.BrewerTypeId = BrewerTypes.BrewerTypeId"
) You are using There are 2 ways to use the query types
To write a defining query for above scenario, modelBuilder.Query<UnitWithStatusQueryType>().ToQuery(
() => Units.Select(u => new UnitWithStatusQueryType(
u.Location.StreetAddress,
u.Brewer.Description,
u.Acquired,
u.OutOfService,
u.Cost))); With above code EF will generate same SQL as what you have put inside |
Thanks for the response and sorry for the delay, i was away. I had already successfully done a regular LINQ query demonstrating the defining query and was moving on to demonstrate doing it using FromSQL. So I do want to stay focused on why the FromSQL doesn't work. My SQL query projects the 5 properties of UnitWithStatusQueryType. As per your comment, I added the last two scalars (locationId and BrewerId) to the SQL query and it works. I want to ensure that I understand the need to project all of the properties of Unit in order to return data to map to the UnitWithStatusQueryType ... which is that like projection queries we do in LINQ, first it needs to project the actual queried type and then perfomr the projection over those results. Is that correct in this case? Also notable, the source of my SQL was what the defining query with the LINQ query (mine was the same as yours) created. It didn't need to do the cascading projections. |
Conceptually, take defining query as LINQ equivalent of view definition in database*. modelBuilder.Query<UnitWithStatusQueryType>().ToQuery(
() => UnitWithStatusQueryTypes
.FromSql(@" SELECT Locations.StreetAddress, BrewerTypes.Description, Units.Acquired, Units.OutOfService,Units.Cost
FROM Units
INNER JOIN Locations ON Units.LocationId = Locations.LocationId
INNER JOIN BrewerTypes ON Units.BrewerTypeId = BrewerTypes.BrewerTypeId"
)); |
thanks. Yeah I had a "v-8" moment in the car (palm slapping forehead) realizing I could have just started with the query type, not the units. (more like a "DUH" moment). But this is good because if I make this logical mistakes, others will also and now I can explain and warn. Glad to see your follow up confirms that. |
@smitpatel that may not be as crazy as you think... unfortunatelly it currently doesn't work: #3932 (comment) |
I've got a defining query with valid SQL that is pulling data from across three tables and then populating a query type with the results.
Here is the defining query:
In both SQlite and SQL Server, the resulting query adds an extra projection that depends on columns not selected in the first projection:
Notice the INNERJOIN on the 2nd to last line.
It is trying to repeat the join on u.BrewerTypeId, but that column is not in u...which is the result of the inner projection. The inner projection returns the correct results.
Let me know if you need to see the entities and query type classes involved.
Further technical details
EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer and Sqlite
Operating system: Windows 10 latest
IDE: Visual Studio 2017 15.7.6
The text was updated successfully, but these errors were encountered: