Zum Inhalt springen

Entity Framework

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design (dotnet ef)
DataContext.cs
public class DataContext(IConfiguration configuration) : DbContext
{
public DbSet<Movie> Movies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(configuration.GetConnectionString("SqlServer"));
}
}
Program.cs
builder.Services.AddDbContext<DataContext>();
appsettings.json
{
"ConnectionStrings": {
"SqlServer": "..."
}
}
DataContext.cs
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"));
}
}

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.

appsettings.json
{
"Serilog": {
"WriteTo": {
"Name": "Console",
"Args": {
"outputTemplate": "..{Message:lj}.."
}
}
}
}