mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
31 lines
1.1 KiB
C#
31 lines
1.1 KiB
C#
|
|
using Microsoft.EntityFrameworkCore; // UseSqlServer
|
|||
|
|
using Microsoft.Extensions.DependencyInjection; // IServiceCollection
|
|||
|
|
|
|||
|
|
namespace Packt.Shared;
|
|||
|
|
|
|||
|
|
public static class NorthwindContextExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Adds NorthwindContext to the specified IServiceCollection. Uses the SqlServer database provider.
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="services"></param>
|
|||
|
|
/// <param name="connectionString">Set to override the default.</param>
|
|||
|
|
/// <returns>An IServiceCollection that can be used to add more services.</returns>
|
|||
|
|
public static IServiceCollection AddNorthwindContext(
|
|||
|
|
this IServiceCollection services,
|
|||
|
|
string connectionString = "Data Source=.;Initial Catalog=Northwind;" +
|
|||
|
|
"Integrated Security=true;MultipleActiveResultsets=true;Encrypt=false")
|
|||
|
|
{
|
|||
|
|
services.AddDbContext<NorthwindContext>(options =>
|
|||
|
|
{
|
|||
|
|
options.UseSqlServer(connectionString);
|
|||
|
|
|
|||
|
|
options.LogTo(WriteLine, // Console
|
|||
|
|
new[] { Microsoft.EntityFrameworkCore
|
|||
|
|
.Diagnostics.RelationalEventId.CommandExecuting });
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
return services;
|
|||
|
|
}
|
|||
|
|
}
|