Support for querying objects without keys
See original GitHub issueNote: 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.
While the FromSql()
method on DbSet<TEntity>
can already be used to bootstrap raw queries which through standard LINQ composition end up projecting arbitrary types (i.e. types that are not mapped in the model), the method requires those queries to be rooted on a mapped type TEntity
.
E.g. assuming Product is an entity type and ProductListEntry is just an arbitrary CLR type that is not mapped in the mode, this works:
var data = db.Set<Product>()
.FromSql("SELECT * FROM Product WHERE 1=1")
.Select(t => new ProductListEntry{Id = t.Id, Name = t.Name})
.ToList();
But this doesn’t:
var data = db.Set<ProductListEntry>()
.FromSql("SELECT Id, Name FROM Product WHERE 1=1")
.ToList();
This item was used initially to track the ability to produce results from raw queries which cannot be expressed as a transformation over a known TEntity
and hence cannot be rooted on a DbSet<TEntity>
.
In the end we decided to enable mapping “query types”, latter renamed to “entities without keys” in the model, which allowed us to support a large portion of the important scenarios this was about. We are now using https://github.com/aspnet/EntityFrameworkCore/issues/10753 to track working with other non-scalar types without having to add them first to the model.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:107
- Comments:62 (17 by maintainers)
Top GitHub Comments
Hello,
Totally agree with @mikary and @Vasimovic.
I’ve found a solution for this while reading the source code. I think it can be used until this issue is solved:
Add this class to your project.
You may use your own query. It works for me…
Pls consider it for release 2.1.0