Skip to content
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

DBQuery() is obsolete in EF core 3? #15656

Closed
amira133 opened this issue May 8, 2019 · 23 comments
Closed

DBQuery() is obsolete in EF core 3? #15656

amira133 opened this issue May 8, 2019 · 23 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@amira133
Copy link

amira133 commented May 8, 2019

hello I want to know DBQuery() is obsoleted in EF core3?
beacuse Visual studio show a message that means DBQuery() is obsoleted!!!
if DBQury is obsoleted what we must used instead of it?

@ajcvickers
Copy link
Member

@amira133 See the breaking change documentation here: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#query-types-are-consolidated-with-entity-types

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label May 8, 2019
@amira133
Copy link
Author

amira133 commented May 8, 2019

Thanks @ajcvickers for your answer.
As I realized , DBSet() should be used instead of DBQuery().
But in DBSet() we must define a primary key , although in DBQuery() we did'nt need to define a primary key ,So how can I prevent from this problem?
please see my whole problem in here.

@ajcvickers
Copy link
Member

@amira133 As it says in the link to the breaking change documentation I posted above, use ModelBuilder.Entity<MyEntity>().HasNoKey().

I would be interested to understand why that wasn't clear from the breaking change documentation? We work hard to make it easy to understand, so if there is something confusing about it, then it would be good to get that feedback.

@amira133
Copy link
Author

amira133 commented May 9, 2019

Thanks @ajcvickers my problem soved!
for your question : we need some example behind breaking change documentation.

@ajcvickers
Copy link
Member

@amira133 What kind of example would be helpful? Can you show the code you would have liked to see?

@Davilink
Copy link

I used the DbQuery to indicate that the query was coming from a view, that way it's clear that we can only read data from that Entity, reverting to a DbSet isn't that breaking that purpose ?

@ajcvickers
Copy link
Member

@amira133 A DbSet for an entity type that is configured with HasNoKey is functionally equivalent to a DbQuery.

@Davilink
Copy link

@ajcvickers Yes but will the intellisense will show you that you cannot Create/Update/Delete that entity ?

@danobri
Copy link

danobri commented Jul 31, 2019

In 3.0, when using the code first approach, how do you define a view with a key? The problem I am running into is that Add-Migration is creating a table that mirrors my view.

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 31, 2019

A view with a key is a table! Remove it from the migration by hand.

@danobri
Copy link

danobri commented Jul 31, 2019

Although my view contains a logical key, I guess there is no need to define that in EF, and by defining a key I am triggering EF to generate a table and primary key. So I removed the [Key] attribute from the class, and added the .HasNoKey() call, which solves the problem. Definitely wouldn't want to manually alter every migration!

@Davilink
Copy link

Davilink commented Aug 1, 2019

I used the DbQuery to indicate that the query was coming from a view, that way it's clear that we can only read data from that Entity, reverting to a DbSet isn't that breaking that purpose ?

Is it an another way to clearly expose the readonly behavior ?

@MuhAssar
Copy link

MuhAssar commented Oct 1, 2019

for future readers, if adding .HasNoKey() didn't prevent the CodeFirst migration from creating a table for that view, try adding the following:
modelBuilder.Entity<User>().ToView("Users").HasNoKey()

as in my situation the migration kept creating a table for the view and using ToView fixed it

@loganmarshall1
Copy link

If you use efcore cli to scaffold from existing db it overwrites the on model building event. So we keep dbQuery in separate partial class works great.

Unfortunately we can't upgrade to 3.0 without a solution.

@zboyles
Copy link

zboyles commented Oct 10, 2019

As @abuassar mentioned, using ToView stoped the keyless table being created. I use ToView(nameof(DbSetName)) for flexibility.

modelBuilder.Entity().ToView(nameof(Users)).HasNoKey();

@craigmoliver
Copy link

@amira133 As it says in the link to the breaking change documentation I posted above, use ModelBuilder.Entity<MyEntity>().HasNoKey().

I would be interested to understand why that wasn't clear from the breaking change documentation? We work hard to make it easy to understand, so if there is something confusing about it, then it would be good to get that feedback.

I would have been helpful if you had documented "a = b":

modelBuilder.Query(); = modelBuilder.Entity().HasNoKey();

@paulirwin
Copy link

This is an unfortunate change from a design standpoint. I really liked the single responsibility aspect of DbQuery. It prevented adding or removing items from the set/query (making a view or table read-only) in a clean and understandable way. It's a "query", not a "set". Perfectly named.

I do not agree with the justification in the breaking changes document. If you were "confused" about this, EF Core 3.0 could have also supported using DbSet with HasNoKey. To deprecate DbQuery (which prevents addition/removal of items) to solve "confusion" in this regard, which implies throwing away the investment in this feature in a future release, does not follow logically. Please un-deprecate this type and continue to support it in addition to the HasNoKey approach.

@ajcvickers
Copy link
Member

@paulirwin The problem with that is that everything gets duplicated when there is only a single very specific semantic change in the model. Literally the only thing that a DbQuery is is a DbSet for an entity type without a key. For example, it's perfectly reasonable to have a DbSet with a key that is read-only. Some things, like being read-only, are implied when there is no key, but that doesn't mean that removing the key is the right way to make a DbSet read-only. The same with mapping to a view, or excluding it from migrations. There are correlations in behavior here, but these things are not inherently coupled to entity types without keys.

@clockwiseq
Copy link

I've read through this issue because I ran into this same issue where I am curious as to why we're removing DbQuery. When I'm grabbing data from another SQL Database, I use a DbContext so EF can do all of the connection magic for me. I use DbQuery to map between a stored procedure or view in a separate database and a partial entity model. This works beautifully and I'm not quite sure how I'll do it now without using the ModelBuilder to specify HasNoKey. An attribute for this would great.

@loganmarshall1
Copy link

loganmarshall1 commented Dec 18, 2019 via email

@Joehannus
Copy link

Joehannus commented Jan 6, 2020

Why was this issue closed? We are using DqQuery too for views. And we'd have to make a lot of breaking changes to a database that has been in production for more than one year. This breaking change is not acceptable.

Also - we don't use the modelbuilder for our migrations, so we don't have a solution for this in our .net core web api. based upon entity framework core (e.g. using modelBuilder.Entity().HasNoKey();). We have a separate application for master data management, based upon DevExpress XAF and asp.net web forms which uses Entiry Framework (using the normal .net framework). In other words we use regular Entity Framework for our Migrations.

Because we have a hybrid approach in which we use regular Entity Framework together with Entiry Framework Core (using DbQuery only in EF Core) this is quite the breaking issue for us.

@loganmarshall1
Copy link

loganmarshall1 commented Jan 6, 2020 via email

@Joehannus
Copy link

Joehannus commented Jan 7, 2020

Completely agree! From: Joehannus notifications@github.com Sent: January 6, 2020 8:09 AM To: aspnet/EntityFrameworkCore EntityFrameworkCore@noreply.github.com Cc: Logan Marshall logan.marshall@live.ca; Comment comment@noreply.github.com Subject: Re: [aspnet/EntityFrameworkCore] DBQuery() is obsolete in EF core 3? (#15656) Why was this issue closed? We are using DqQuery too for views. And we'd have to make a lot of breaking changes to a database that has been in production for more than one year. This breaking change is not acceptable. — You are receiving this because you commented. Reply to this email directly, view it on GitHub<#15656?email_source=notifications&email_token=AEYSEOGLYFG7D6765AKMMTTQ4MUOJA5CNFSM4HLSSGO2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIFMPQI#issuecomment-571131841>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEYSEOGPOWTAMJLC4PWPYRTQ4MUOJANCNFSM4HLSSGOQ.

I opened a new issue for this (#19509)

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests