You can easily add the IServiceCollection extension method to your business / service level and use it to register your dependencies. Then, during the startup process, you call the method at the service level without reference to the EntityFramework in your web application.
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace your.service.layer { public static class MyServiceCollectionExtensions { public static IServiceCollection AddMyServiceDependencies(this IServiceCollection services, string connectionString) { services.AddEntityFrameworkSqlServer() .AddDbContext<YourDbContext>((serviceProvider, options) => options.UseSqlServer(connectionString) .UseInternalServiceProvider(serviceProvider) ); return services; } } }
Startup:
using your.service.layer; public void ConfigureServices(IServiceCollection services) { var connectionString = Configuration.GetConnectionString("EntityFrameworkConnectionString"); services.AddMyServiceDependencies(connectionString); }
Now your web application only needs a link to your business / service level, and it does not depend directly on EntityFramework.
source share