using Microsoft.EntityFrameworkCore; // UseSqlite
using Microsoft.Extensions.DependencyInjection; // IServiceCollection
namespace Packt.Shared;
public static class NorthwindContextExtensions
{
///
/// Adds NorthwindContext to the specified IServiceCollection. Uses the Sqlite database provider.
///
///
/// Set to override the default of ".."
/// An IServiceCollection that can be used to add more services.
public static IServiceCollection AddNorthwindContext(
this IServiceCollection services, string relativePath = "..")
{
string databasePath = Path.Combine(relativePath, "Northwind.db");
services.AddDbContext(options =>
{
options.UseSqlite($"Data Source={databasePath}");
options.LogTo(WriteLine, // Console
new[] { Microsoft.EntityFrameworkCore
.Diagnostics.RelationalEventId.CommandExecuting });
});
return services;
}
}