If using .NET Framework 5 with Entity Framework Core5 and like to configure your connection string and logging see the following GitHub repository.
Log options (which can be modified easily)
Code:
Public Enum LoggingDestination
DebugWindow
LogFile
None
End Enum
Settings stored in appsettings.json in frontend project (data operations in this case are in a class project)
Code:
{
"database": {
"DatabaseServer": ".\\SQLEXPRESS",
"Catalog": "School",
"IntegratedSecurity": "true",
"UsingLogging": "true",
"LoggingDestination": "LogFile",
"LogFileName": "karen.txt"
}
}
Connection string and logging used in a DbContext
Code:
Protected Overrides Sub OnConfiguring(ByVal optionsBuilder As DbContextOptionsBuilder)
If Not optionsBuilder.IsConfigured Then
Select Case Helper.LoggingDestination()
Case LoggingDestination.DebugWindow
LogQueryInfoToDebugOutputWindow(optionsBuilder)
Case LoggingDestination.LogFile
LogQueryInfoToFile(optionsBuilder)
Case LoggingDestination.None
NoLogging(optionsBuilder)
Case Else
Throw New ArgumentOutOfRangeException()
End Select
End If
End Sub
To use, run the data script to create/populate the database.
In Visual Studio, perform a NuGet Restored packages.
In appsettings.json, change LogFileName element to the name of the log file to write too.
Run, project closes, open the log file specified above.
NuGet packages
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.Extensions.Configuration.FileExtensions
- Newtonsoft.Json
- Microsoft.Extensions.Configuration.Binder
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.Configuration