Bring back DbQuery<T>
See original GitHub issueIn 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:
- Created 4 years ago
- Reactions:11
- Comments:15 (6 by maintainers)
@ErikEJ Sorry my bad, now I found that a OnModelCreatingPartial method is generated, that one does the trick, thanks a lot!
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.