Home > Backend Development > C++ > How Can I Execute Raw SQL Queries in Entity Framework Core Without Using DbSet?

How Can I Execute Raw SQL Queries in Entity Framework Core Without Using DbSet?

Patricia Arquette
Release: 2025-01-27 01:06:10
Original
607 people have browsed it

How Can I Execute Raw SQL Queries in Entity Framework Core Without Using DbSet?

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 and higher versions

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.

EF Core 3.0

SqlQuery

For EF Core 3.0, you can use the keyless entity type (previously called the query type). These types do not map to a specific database table and allow any data retrieval. To define the key -free physical type, use the

data annotation or method in your model configuration.

EF Core 2.1

[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

attribute in your DBContext to represent the query type, and then use the

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template