Entity Framework Core (EF Core for short )is a popular ORM (object-relational mapper) from Microsoft that enable you to carry out waste operations (create, check out, update, and delete) without needing to understand how the data is persisted in the underlying database.When working with ORMs, we frequently take advantage of models that are mapped to database tables. Nevertheless, what if we have a design that doesn t simulate a database table? How can we map non-entity types and populate objects of such a model in our applications? We can achieve this with query types.Initially introduced in EF Core 2.1, query types are non-entity types(
classes)that can map to tables or views in the database without an identity column specified, meaning tables and views that lack a key. EF Core inquiry types make it simpler to query views and design types that wear t require identity columns. Nevertheless, due to the fact that inquiry types don t have an identity column defined, you can not place, update, or erase information using them. You can utilize them just to recover the data.Query types enable you to specify a mapping between a database question and your domain classes.
You can then use the same inquiry type with various types of EF Core inquiries, such as LINQ to Entities or EF Core Migrations. This post goes over how we can use EF Core query types in ASP.NET Core 7 applications. To work with the code
examples offered in this short article, you must have Visual Studio 2022 Sneak peek set up in your system. If you wear t currently have a copy, you can download Visual Studio 2022 here. Produce an ASP.NET Core 7 Web API project in Visual Studio 2022 To begin with, let s produce an ASP.NET Core Web API job in Visual Studio 2022.
Following these actions will produce a new ASP.NET Core Web API job in Visual Studio 2022: Launch the Visual Studio 2022 IDE. Click Develop new task. In the Create new project window, choose ASP.NET Core Web API from the list of design templates showed. Click Next. In the Configure your new task window, specify the name and area for the brand-new job. Additionally inspect the Place service and task in the exact same directory check box, depending upon your preferences.
public int Id get; set; public string FirstName < public class Batch p> public int Id get; set; public string Title. get; set; public int NoOfStudents get; set; public int TeacherId. get; set;
can be utilized to store the information recovered
from the view we just produced. The following code bit shows how you can produce a class named BatchDetails to keep the data
queried from the view. public class BatchDetails Set up the inquiry type in EF Core You have 2 methods to configure the inquiry type.
If you want to avoid cluttering your DbContext, you can develop your DbContext class as a partial class and after that split the DbQuery statement into a separate file altogether.Here is the material of the DemoDbContext.cs file: public partial class DemoDbContext: DbContext And here is the content of the DemoDbContextQuery.cs file: public partial class DemoDbContext: DbContext You must configure the question key in the OnModelCreating method as shown in the code snippet provided below. safeguarded override void OnModelCreating (ModelBuilder modelBuilder
create a repository to read information from the database. Note that
, while the repository will connect with the database straight, the controller will utilize the repository to get information.(We ll carry out the controller in the next section.
)Produce a file named IBatchRepository.cs with the following code. IBatchRepository will act as the interface for our BatchDetailsRepository. public user interface IBatchDetailsRepository Produce another file called BatchDetailsRepository.cs and get in
the following code to produce the repository class. public class BatchDetailsRepository: IBatchDetailsRepository p>
Create an API controller in ASP.NET Core Now, develop an API controller called BatchDetailsController in a file with the same name and a.cs extension. Then compose the following code in there. [Route( “api/ [controller])] [ApiController] public class BatchDetailsController: ControllerBase private IBatchDetailsRepository _ batchDetailsRepository; public BatchDetailsController(IBatchDetailsRepository batchDetailsRepository). _ batchDetailsRepository=batchDetailsRepository;. [. HttpGet]. public IActionResult Get()return Ok( _ batchDetailsRepository.GetBatchDetails( )); Refer to the preceding code listing. Note how dependency injection has been used to inject a circumstances of type IBatchDetailsRepository in the fitter of the BatchDetailsController class.You can return inquiry types from raw SQL queries utilizing the FromSql approach in the DbQuery type and they can participate in relationships as well.Finally, there are two constraints of question types you ought to remember. As mentioned above, due to the fact that inquiry types can not be tracked by the context, you can just utilize them for checks out, not composes. And you can not utilize the Include and Connect approaches of the DbContext when dealing with query types. Copyright © 2022 IDG Communications, Inc. Source