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.

Bring back DbQuery<T>

See original GitHub issue

In EF Core 2.2, we used a query type similar to this (notice the Id property):

public sealed class Record
{
    public int Id { get; set; }
    public string Value { get; set; }
}

that wasn’t mapped to anything - not a table, not a view. We only used it with FromSql(). In our DbContext we had:

public DbQuery<Record> Records { get; set; }

After upgrading to EF Core 3.0, I see that DbQuery is marked as obsolete, suggesting the use of DbSet instead. I was a little confused, because - how will EF Core then be able to tell that this isn’t actually supposed to be an entity… only a query type? And of course after running this, EF Core suddenly treated it as an entity and wanted to create a table for Records.

I saw a suggestion to mitigate this by configuring the type using HasNoKey(). That didn’t work. EF Core still wanted to create a table for it. So I tried a few other things - calling ToTable(null) since we don’t want a table, but that threw an exception. Eventually I tried using ToView (even though we don’t want to map this to any view) and calling ToView(null) finally did the trick.

My question is: why the deprecation of DbQuery<T>?

The docs says:

This change was made to reduce the confusion around the purpose of query types.

In my opinion, the previous behavior was really clear and unambiguous - in our DbContext, we had DbSet<T> and DbQuery<T> properties. You could immediately see what was an actual entity mapped to a table in the database (DbSet) and what was not (DbQuery).

Now, I can’t tell by looking at the DbContext itself. Instead of a simple change in the type of a property, I have to add additional configuration for the type to prevent it from being treated as an entity and mapped to a table, in a way that’s not intuitive at all. I find the current state much more confusing than before.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
loth0commented, Nov 12, 2019

@ErikEJ Sorry my bad, now I found that a OnModelCreatingPartial method is generated, that one does the trick, thanks a lot!

0reactions
cda-1commented, Jan 7, 2020

@ajcvickers

What is the reason you need to DbSet properties for these types on your DbContext?

We don’t. I think the author wrote it that way to avoid having to use fluent configuration (which we now have to use anyway to prevent EF Core from trying to create a table for it but at the same time be able to use it in FromSql). So yes, now it’s pointless, I’ll remove them, thanks.

I’m following this thread and the exchange between @Neme12 and @ajcvickers, but I don’t understand this part. Is the DbSet property still (in .net core 3.1) required if we are using .FromSql()?

I am still using FromSql, so I’ll still need the DbSet item, and .HasNoKey() and .ToView(null), right?

I created a Stack Overflow Question about this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bring back DbQuery<T> · Issue #18719 · dotnet/efcore
In EF Core 2.2, we used a query type similar to this (notice the Id property): public sealed class Record { public int...
Read more >
Returning DbQuery to view that requires IEnumerable
The problem is that you trying to return ObjectQuery from within the using block. Try to materialize your object-set
Read more >
Reverting your SQL Server database back to a specific point ...
Reverting your SQL Server database back to a specific point in time can be easily done with ApexSQL Log.
Read more >
dbquery and outputcsv limited to 10000 results
Solved: I am trying to create a lookup file daily from data I am pulling with dbquery out of an Oracle Database.
Read more >
Querying data via the DbSet
The query brings back all authors, their books, and each book's publisher. The Include method is only effective if you return entity types....
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