-
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
Query Types: Model types that do not require identity #9290
Comments
Table valued functions can return an arbitrary type. Usually this result is then cross applied with other parts of the query, but it can just be selected. I need a way to map this type and I think this feature could help with that. |
@pmiddleton that is exactly right. I will add it to the list or related features. |
In this context, you should also support temporary tables. |
The lack of views support is still a show stopper for me as my solutions heavily depend on them. |
@tdjastrzebski Can you elaborate on what isn't working for you? You can already map entity types to database views and it will work fine (provided the views are updatable) - We make no distinction between views and tables when generating SQL during query or update. This feature is not really about database views, but about being able to model key-less structural types. |
@anpete I assumed views were not supported at all since entity classes were not generated - at least not using Scaffold-DbContext. Maybe there should be a switch added? |
Tables/Views distinction makes no sense. Drop this concept please. |
RE: Those with no PK will remain read-only @tdjastrzebski, however, there can be read-only views and stored procedures that "do" return data with PKs; IMHO using the PK to derive (read-only/write access) may not be the best option; perhaps this would work for you #1862 |
@Vasimovic correct, this may not be 100% reliable option. Similarly, scaffolding will perhaps never be 100% correct with that respect simply because new providers will be implemented with functionality we cannot even predict yet. So why bother with special view classes? Just so the developer does not get exception when he/she tries what is not supported? |
It just seems freaky to be lacking support for such a fundamental thing this far down the line. |
I vote for this feature. I am using EF Core 2.0 to load results from a view I'm using as part of a complex search. The view does not have a primary key. In order to satisfy Entity Frameworks primary key requirement I lied and told EF that a column is a primary key. EF produced the result-set and all looked OK at first, but I found that when the "key" column contained duplicates - EF had replaced all subsequent rows with an exact copy of the first row for each "key" value!!! There was no warning, no error! OK, I did lie about the key, so I suppose EF has the right to lie back to me. My eventual workaround for this was to manufacture an ID column using ROW_NUMBER() and use that column as the key. So, Entity Framework seems needy requiring this key definition. I vote to have a View Type (or possibly call it a Report type or a Read-Only type). This entity type would not care about keys or change tracking, so it could be simple and light-weight. |
@renbud Then how updatable views should be treated? |
@tdjastrzebski Again, EF Core does not care whether you are mapping entity types to database tables or updatable views. Scaffolding does not support views, but you can easily hand write entity types and map them perfectly well to views (via ToTable) - Yes, ToTable is somewhat confusing here, but the way to think about this is that it is simply allows you to configure the name of the database object that EF will use when generating SQL. Additionally, in 2.1 we are adding Query Types, which allow you to define model types that do not have identity. These can also be mapped to tables or views in the database but are never tracked and are therefore essentially read-only. @alexzaytsev-newsroomly Expect docs to accompany the release. For now, you can take a look at our tests: https://github.com/aspnet/EntityFrameworkCore/blob/dev/src/EFCore.Specification.Tests/TestModels/Northwind/NorthwindContext.cs |
Created #10753 to track the remaining work here. |
@anpete Will we still need to define a DbQuery with Stored Procedures? I was hoping for something like FromSql (FromQuery) without the DbSet to map to POCO classes |
@Vasimovic No, you don't need a DbQuery property on your context (if that's what you mean). You do need to register the query type in OnModelCreating, via You can use FromSql in the same way as with entity types. E.g. |
@anpete Just so I get this right: So |
The 2.1.0 does not seem to be available from the nuget.org prerelease source feed within Visual Studio 2017. Do I have to build from source if I want to try QueryType now? |
@samuelan Prerelease builds are only available on myget, as described here. So you should probably try the version from the aspnetcore-dev feed |
Thanks, @poke . Will try it out. |
@anpete I don't see how this will work without having a baseline DBSet object that allows primary key. For me, this requirement should mean I can query a view as read only and not require a DBSet object to do so. I am unable to do that in the 2.1 version. I am working with existing database views and I am thinking I just don't understand the full functionality here. Any ideas? |
@emmielewis Are you using the nightly builds? What is not working exactly? |
@anpete - I am using build 2.1.0-preview2-t000. I looked at your example and set it up in a similar fashion but your example is using a DBSet object to build a query. It works when I do that. In my case, I have an existing view that I want to represent as an object and don't want to use the DBSet object (avoid primary key requirement) in order to create a query. How do I use DBQuery with an existing database view without any primary key requirements at all? Do I have to use a DBSet object to build this in EF? |
@emmielewis You have hit a quirk of our APIs here 😄 You map a Query Type to a view by using |
@anpete - Ok. I think I got confused from the example. I just saw your note above that says "ToTable" is a bit confusing. I am trying this out now. |
@anpete - Thank you! That works great for me! |
@emmielewis Hard to say. Is there a lot of data? It uses the same code paths as regular entity queries and so perf. should be the same. |
@anpete - I got it resolved. This is working great. Thank you! |
modelBuilder.Query().ToTable("TestView", "TestSchema") is not working in the https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/2.1.0-preview2-final in Nuget. It looks like you all changed the name to ToView. Can you all update your documentation with this information? |
@emmielewis - QueryType.ToTable API has been renamed to QueryType.ToView in 2.1.0-preview2 |
Yep. I just noticed that. Can you all update your documentation with this information? https://docs.microsoft.com/en-us/ef/core/modeling/query-types |
cc: @divega |
@smitpatel, @emmielewis I created dotnet/EntityFramework.Docs#682. |
I have had similar problems. I want to return a model or type that is not in the DBContext. So, I return it as Json from SQL 2016: Add a table to the database with a key (int or guid) and a string value public class JsonReturn I then use that model with a FromSql statement then I parse the Json out into the ViewModel or DIO that I need.
This would work with views as well would it not? Even an update statement. Am I just looking at this to simply? |
Note: The feature tracked in this issue could help with using EF Core with database views. However, the feature is not limited to database views and its completion would not mean that every aspect of database view support has been implemented. See #827 for an overview of the areas where EF Core interacts with database views.
We want to enable modeling types that exist independently of entities but have no keys (and therefore are not updatable) as first class objects in EF Core models.
Relationship between query types and other features:
FromSql()
or defining query, having result types that don't need to have keys makes the feature much more useful.Alternative names for the feature
We can consider renaming the feature to something else if we find a better name. So far the list is short:
The text was updated successfully, but these errors were encountered: