No need to DBSET in Entity Framework Core to execute the original SQL query
In Entity Framework Core, the primitive SQL query that does not depend on DBSET becomes more challenging. The traditional
method has been removed, and developers need to find alternative methods to use SQL query search data.
dbData.Database.SqlQuery<somemodel>
EF Core 8 introduces the method to support the return the scalar value and any type from the original SQL query. This method allows you to perform the SQL query directly and map the results to the required type.
SqlQuery
data annotation or method in your model configuration.
[Keyless]
HasNoKey()
In EF Core 2.1 Release Candidate 1, the query type was introduced. These types are used as the return type of temporary query, which are mapped to the database view or table without the main key. Define a
method to execute the query. Example:
Summary FromSql()
DbQuery<t>
FromSql
These methods provide different methods to perform the original SQL query in Entity Framework Core without relying on DBSET. Select the method that is best for you and EF Core version.
The above is the detailed content of How Can I Execute Raw SQL Queries in Entity Framework Core Without Using DbSet?. For more information, please follow other related articles on the PHP Chinese website!