Entity Framework Core( EF Core)is a modern-day, open-source, object-database mapper that streamlines information gain access to for.NET applications. EF Core allows you to deal with information from a variety of sources including relational databases, non-relational databases, and even in-memory data.EF Core allows you to compose code to carry out CRUD actions (develop, read, update, and erase) without understanding how the data is persisted in the underlying database. Utilizing EF Core, you can recover entities from an information shop, add or alter or delete entities, and traverse entity graphs.In other words,
EF Core simplifies your data gain access to by permitting you to write code that performs those waste operations using.NET objects, without directly connecting with the underlying database supplier. This post discuss how you can utilize Entity Structure Core to log database activity when working in ASP.NET Core 7 applications.To work with the code examples provided in this post, you ought to have Visual
Studio 2022 Preview installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 Sneak Peek here. Produce an ASP.NET Core very little Web API job in Visual Studio 2022 Sneak Peek First off,
let’s produce an ASP.NET Core project in Visual Studio 2022. Following these actions will develop a brand-new ASP.NET Core Web API 6 job in Visual Studio 2022: Launch the Visual Studio 2022 Preview IDE. Click” Produce brand-new project.” In the” Develop new job”window, choose “ASP.NET Core Web API “from the list of templates displayed. Click Next. In the “Configure your new job “window, specify the name and place for the brand-new project.
sections of this article. Logging choices in Entity Structure Core In Entity Framework Core, logging is used to track database inquiries and other operations.
Entity Framework Core utilizes the Microsoft.Extensions.Logging structure to log events. This framework supplies a wide range of logging suppliers that can be utilized to tape log messages.By default, Entity Framework Core will compose log messages to the console. In addition to using the Microsoft.Extensions.Logging structure, Entity Framework Core also supports third-party logging frameworks such as NLog and Serilog. These frameworks can be used to write log messages to files, databases, or other destinations. If you mean to use EF Core and SQL Server in your application, you will need to add the Microsoft.EntityFrameworkCore.SqlServer NuGet plan to your task. Select the project in Solution Explorer, then right-click and choose”Handle NuGet Plans.”In the NuGet Bundle Manager, look for Microsoft.EntityFrameworkCore.SqlServer and install the package.Alternatively, you can set up
the plan by means of the NuGet Plan Supervisor console by entering the command revealed below.PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer Develop a Consumer class in.NET Core Produce a class called Consumer in a file having the exact same name with a “. cs”extension and compose the following code in there: public class Customer public int Id < public string FirstName get; set;=string.Empty; public string LastName <=string.Empty; Set up visiting Entity Structure Core
You can take advantage of visiting Entity Framework Core in either of two ways: Utilize the UseLoggerFactory extension method. Use the ILoggerProvider interface. The UseLoggerFactory extension technique is the suggested way to configure logging in EF Core, as it allows for a more flexible setup. To utilize the UseLoggerFactory extension approach, just include it to your DbContext class as shown in the code bit provided listed below. using Microsoft.Extensions.Logging;
public class MyDbContext: DbContext li>
; Database=DemoDb; Trusted_Connection=Real;” ); To enable EF Core to log data to the console, you can utilize the following code.
optionsBuilder.UseSqlServer(“Server =JOYDIP; Database= DemoDb; Trusted_Connection=Real; “). LogTo(Console.WriteLine ).