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 ".." /// Set to override the default of "Northwind.db" /// An IServiceCollection that can be used to add more services. public static IServiceCollection AddNorthwindContext( this IServiceCollection services, string relativePath = "..", string databaseFilename = "Northwind.db") { string databasePath = Path.Combine(relativePath, databaseFilename); services.AddDbContext(options => { options.UseSqlite($"Data Source={databasePath}"); options.LogTo(WriteLine, // Console new[] { Microsoft.EntityFrameworkCore .Diagnostics.RelationalEventId.CommandExecuting }); }, // Register with a transient lifetime to avoid concurrency issues with Blazor Server projects. contextLifetime: ServiceLifetime.Transient, optionsLifetime: ServiceLifetime.Transient); return services; } }