EntityFrameworkCoreHelpers 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EntityFrameworkCoreHelpers --version 1.0.0
NuGet\Install-Package EntityFrameworkCoreHelpers -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityFrameworkCoreHelpers" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCoreHelpers --version 1.0.0
#r "nuget: EntityFrameworkCoreHelpers, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityFrameworkCoreHelpers as a Cake Addin
#addin nuget:?package=EntityFrameworkCoreHelpers&version=1.0.0

// Install EntityFrameworkCoreHelpers as a Cake Tool
#tool nuget:?package=EntityFrameworkCoreHelpers&version=1.0.0

About

Provides the following methods for connecting to a SQL-Server database.

Note

Current uses Microsoft.EntityFrameworkCore.SqlServer version 5.0.9, next release will accept higher releases of this package.

Place the following appsettings.json file into a project, set to copy if newer

  1. Populate each environment with appropriate connection strings.
  2. Set ActiveEnvironment to the desired environment.
  3. Use one of the connection methods below in your DbContext class.
{
  "ConnectionsConfiguration": {
    "ActiveEnvironment": "Production",
    "Development": "Dev connection string goes here",
    "Stage": "Stage connection string goes here",
    "Production": "Prod connection string goes here"
  }
}

Connect without logging

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        DbContextConnections.NoLogging(optionsBuilder);
    }
}

Connect with logging via Debug.WriteLine

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        DbContextConnections.StandardLogging(optionsBuilder);
    }
}

Connect with logging to a folder named Logs under the current application folder.

Note the folder Logs must exist while the log file does not need to exists. To ensure the folder exist consider a msbuild task (see below)

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        DbContextConnections.CustomLogging(optionsBuilder);
    }
}
public class DbContextConnections
{
    /// <summary>
    /// Simple configuration for setting the connection string
    /// </summary>
    public static void NoLogging(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(ConfigurationHelper.ConnectionString());
    }

    /// <summary>
    /// Default logging to output window
    /// </summary>
    public static void StandardLogging(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(ConfigurationHelper.ConnectionString())
            .EnableSensitiveDataLogging()
            .LogTo(message => Debug.WriteLine(message));
    }
    /// <summary>
    /// Writes/appends to a file
    /// Make sure that the folder exists, as coded folder name is Logs under the app folder.
    /// One way to ensure the folder exists is to use MsBuild task MakeDir as in
    /// the test project ShadowPropertiesUnitTestProject.
    /// </summary>
    public static void CustomLogging(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(ConfigurationHelper.ConnectionString())
            .EnableSensitiveDataLogging()
            .LogTo(new DbContextLogger().Log)
            .EnableSensitiveDataLogging()
            .EnableDetailedErrors();
    }
}

msbuild MakeDir

<Target Name="MakeLogFolder" AfterTargets="Build">
	<MakeDir Directories="$(OutDir)Logs" />
</Target>
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 631 4/24/2022
1.0.0 401 4/24/2022

All code works. Current constrained to 5.0.9 for Microsoft.EntityFrameworkCore.SqlServer package. Next release will resolve this constraint.