-
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
Support raw SQL queries without defining an entity type for the result #10753
Comments
@anpete I see where this allows raw sql to be run for a given context (existing class), but what about raw sql that is custom? Is there a way to just run a SQL command (e.g. a select) and get an object that contains the resulting rows (regardless of what tables the command includes)? |
@mrlife A Query Type can be any shape you want. Define the type; add it to the model as a Query Type; and then query it using FromSql. |
@anpete That's definitely nice to have. I think the heart of what I'm looking for is a way to skip creating any model classes and just run a query and get a result. Are there any plans to support that, either in EF Core or in some way within an ASP.NET Core project? |
@mrlife What would be the type of the result? |
@anpete This kind of query is a huge part of our business, where we use I suppose there are many ways to approach how to dynamically create one or more objects to hold whatever is returned from the database (based on detected database column types). That's probably a long discussion on a desirable way to do that. It's not clear if there are any options currently available in ASP.NET Core that handle this. Examples in .NET: |
@mrlife As you point out, ADO is the currently recommended way of doing this. NB: You can easily integrate ADO usage with EF, take a look at the DbContext.Database property. It sounds like you want EF to do something here. Given that you don't want any mapping capabilities, what exactly would EF be adding over ADO? Perhaps providing an example code snippet would help. |
@anpete An ORM by itself would not provide any value with the types of queries we're running, however if EF Core with ADO is the way in ASP.NET Core to run any SQL and get a result set, then that's what we need. I looked at Configuring a DbContext, but it's not clear how to use it without an explicit model file. Is that possible? |
@mrlife It is completely fine to just use ADO from ASP.NET Core - you don't need to use EF at all. |
RE: it may be possible to lazily add a type to the model on first access That would be fantastic; although, I was wondering if you could also give us the option to do add a DbQuery at runtime ourselves as well Something like:
|
@anpete From your last comment, I'm having trouble finding documentation for this for aspnet core. Are you aware of any? |
@mrlife - There is nothing special in ASP.NET core when it comes to using ADO.NET provider directly. We are not aware of any docs for that. But it wouldn't be much different from how you would use ADO.NET provider from any code. |
Is there any way I could implement this in EF I was looking for a way to add DbQuery to the DbContext at runtime |
@Vasimovic What do you mean by, "implement this in EF 2.2"? Do you mean can you implement this in the EF code and send a PR? Or do you mean, can you implement something on top of EF in your own code that will allow types to be added to the model dynamically? |
Hi @ajcvickers, sorry, I meant 2.1, and yes something on top of EF in my own code that will allow types to be added to the model dynamically |
@Vasimovic The main limitation in EF Core to be aware of is that the model is read-only once it is being used by a context instance. (Mostly because multiple threads may be accessing it and mutation is not thread-safe.) So, the way to do this now is to build a new model when you need to add a new type, then create a new context instance that uses that model. Whether or not this is practical depends mostly on performance. That is, building a model is expensive, and caching many models will use a lot of memory. |
I need it in the same context, I am in the process of migrating a project from EF 6 to EF Core and have about 200 view-only models, adding the DbQuery to the context for 200 of them seemed a bit 😄 - Anyhow #10753 will be very useful for me Thank you for taking your time to respond @ajcvickers much appreciated |
Hi @ajcvickers, |
@jenergm The milestone for each issue indicates the release in which a fix for the issue is planned to ship. Currently we have tentative planning (subject to change) for two releases: 2.2, and 3.0. Issues, like this one, that are in the Backlog milestone are planned to be post 3.0. |
Hi @ajcvickers. Thanks for sharing the roadmap. |
Hi @ajcvickers , how's going? Are you know if SqlQuery<TElement> will be released in that next version of .NET Core? Best regards, |
@jenergm Not as currently planned. |
Fixes #10753 Builds an ad-hoc entity type and uses it to query when `SqlQuery` is called for a type that does not have a type mapping. Things to consider: - The entity type cannot have relationships - Properties are mapped by convention and mapping attributes are respected. - The entity types are keyless.
Fixes #10753 Builds an ad-hoc entity type and uses it to query when `SqlQuery` is called for a type that does not have a type mapping. Things to consider: - The entity type cannot have relationships - Properties are mapped by convention and mapping attributes are respected. - The entity types are keyless.
Amazing. So this will be in daily build now? |
Thank you @ajcvickers! I think it is this: Very good! Did it already have released? |
@ErikEJ Should be. @jenergm As the milestone indicates, it will be released in EF Core 8. However, it should be available now in the daily builds and will be included in preview 1 of EF8. |
@ajcvickers just tried this feature today and I love it! This can easily replace Dapper for many and allow us to incrementally adopt EF Core. One question though: will it support scalar values? This doesn't seem to work (only tested with npgsql). _db.Database.SqlQuery<string>($"SELECT id FROM environments WHERE key = {appKey}").FirstOrDefault(); |
That would be awesome! Waiting for more info. |
@ajcvickers I've created a new issue showing this error: #30447 In my case I'm using SqlServer. The issue is that EF generates this code:
instead of:
|
Thanks @Misiu thsts the same error message I get using Postgres |
@ajcvickers I have posted related Q on SO: |
@borisdj The linked question is confusing overall, but it adds a class definition to the DB context - so no, it's not directly relevant to this ticket - which is specifically about non-entity result types |
would it be possible to show an example of how to call a stored procedure with this new functionality? |
Thanks for that information. It seems that EF8 will give some exciting new
potential. My only comment is that when you mention in the docs ' The
code shown here comes from RawSqlSample.cs.'
Although it does seem to be part of NewInEFCore8 project, I have never been
able to download a specific project. They all seem to be part of the
'EntityFramework.Docs' solution which is now up to what seems like 100
terabytes. It would be very useful to be able to 'clone' individual
projects.
Thanks for your help
Regards,
PatC
…On Tue, Oct 3, 2023 at 11:28 AM Arthur Vickers ***@***.***> wrote:
@pcapozzi <https://github.com/pcapozzi> See
https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew#raw-sql-queries-for-unmapped-types
—
Reply to this email directly, view it on GitHub
<#10753 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABE7UL5F3Q5EEKPPBQNZBLX5PLDHAVCNFSM4ENJ43T2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZUGQ2TQMZVGE4A>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
This is added, so great. I want to add that this is essential for calls that use FreeTextTable() and ContainsTable() in SQL Server. |
For Query Types it could be nice to not require a configuration call in OnModelCreating to add the type to the model. Instead, it may be possible to lazily add a type to the model on first access (usually query).
The text was updated successfully, but these errors were encountered: