question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cosmos: remove automatic composition of discriminator filter in raw SQL queries

See original GitHub issue

To ensure the correct type is project for materialization, FromSqlRaw wraps the query passed, so that this:

var contacts = await context.Contacts.FromSqlRaw("select * from Contacts").ToListAsync();

Really gets resolved as this:

SELECT c FROM (SELECT * From Contacts) c WHERE c.Discriminator = "Contact"

While this works fine in most scenarios, it causes issues when users project explicit columns. Although this works fine as a raw query and returns multiple records:

SELECT Id, Name, Phone, Address FROM Contacts

This will never return any records and doesn’t throw an error or log a warning:

var contacts = await context.Contacts.FromSqlRaw("select Id, Name, Phone, Address from Contacts").ToListAsync();

Although this behavior is documented, it’s not obvious if the documentation hasn’t been read. It could lead to a lot of troubleshooting around a non-issue that just requires the user add one column to their select statement.

I suggest either logging a warning or throwing an exception when Discriminator isn’t included so it’s clear to the user it won’t behave as expected and what to do for mitigation.

EF Core version: EF Core 6.0 Database provider: All Target framework: .NET 6.0 Operating system: BeOS IDE: Visual Studio 2022

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Sep 23, 2021

@roji Nominating you to do this for 6.0. 😃

1reaction
AndriySvyrydcommented, Sep 23, 2021

Yes, otherwise it would be a “breaking” change

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot query issues when using Azure Cosmos DB
Learn how to identify, diagnose, and troubleshoot Azure Cosmos DB SQL query issues.
Read more >
Throw when using relational FromSqlRaw with Cosmos, or ...
In NET6 EF Core I get an compile error if I use the method FromSqlRaw \ FromSqlRawAsync when both of the following NuGet...
Read more >
CosmosDB document client - how to implicit add type for ...
1 Answer 1 ... Here, you can directly add your query which will filter based on Discriminator field. ... Thank you for answer,...
Read more >
Cosmos db select where. How can a make a query to SELECT ...
Cosmos DB SQL API > Filter out child array objects with specific values. I think you want to change the select statement to...
Read more >
An in-depth study of Cosmos DB and the EF Core 3 to 5 ...
This article looks at the capabilities of Cosmos DB database when used via the new EF Core 3.0 database provider.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found