Entity Framework
Install Nuget
Abschnitt betitelt „Install Nuget“- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Design (dotnet ef)
public class DataContext(IConfiguration configuration) : DbContext{ public DbSet<Movie> Movies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(configuration.GetConnectionString("SqlServer")); }}builder.Services.AddDbContext<DataContext>();{ "ConnectionStrings": { "SqlServer": "..." }}Enable Sql Logging
Abschnitt betitelt „Enable Sql Logging“public class DataContext(ILoggerFactory loggerFactory, IConfiguration configuration) : DbContext{ public DbSet<Movie> Movies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder#if DEBUG .UseLoggerFactory(loggerFactory) .EnableSensitiveDataLogging() .EnableDetailedErrors()#endif .UseSqlServer(configuration.GetConnectionString("SqlServer")); }}Serilog Output Template
Abschnitt betitelt „Serilog Output Template“Explanation :lj Specifier
Abschnitt betitelt „Explanation :lj Specifier“The :lj (literal JSON) specifier tells Serilog to render the message with its line breaks rather than escaping them. This means that any \r\n characters in your EF Core SQL logs will be rendered as actual newlines in the output.
{ "Serilog": { "WriteTo": { "Name": "Console", "Args": { "outputTemplate": "..{Message:lj}.." } } }}