Enabling LINQ to SQL Output with .NET Entity Framework

By David Catino (Profile).


Stack Overflow Reference

MSDN Documentation

Enabling LINQ to SQL output in Entity Framework is straightforward. Simply add additional options when configuring the DbContext within the `Use...` context dependency injection definition found in the `Program.cs` file.

LINQ to SQL Output Example
As seen in the screenshot (context name redacted).

This example uses SQLite as the data source to create proof-of-concepts without complex database setup instructions.

    
      // Change this value for deployment (security risk!)
      bool isDebugMode = true;

      // Reference: https://stackoverflow.com/questions/23804783/log-queries-executed-by-entity-framework-dbcontext
      // Enables output to the console
      builder.Services.AddDbContext(options =>
      {
          options.UseSqlite(
              builder.Configuration.GetConnectionString("NOPEContext") ??
              throw new InvalidOperationException("Connection string 'NOPEContext' not found."))
          .LogTo(s => System.Diagnostics.Debug.WriteLine(s))
          .EnableDetailedErrors(isDebugMode)
          .EnableSensitiveDataLogging(isDebugMode);
      });