0

I have a .net Core 3.1 project, which uses a class library for DAL and a Class library for the BLL layer and a Core 3.1 MVC project.

The DAL project registers the BLL interface with the the DAL class as the implementation type. It uses ADO and not EF.

public static class ServiceCollectionExtensions
{
    // Add parameters if required, e.g. for configuration
    public static IServiceCollection AddDAL(this IServiceCollection services)
    {
        // Register all services as required
        services.AddScoped<ITenementLeaseBll, TenementLeaseDal>();

        return services;
    }
}

The "AddDal" is registered in the startup.cs (ConfigureServices(IServiceCollection services) as

services.AddDAL();

Following some posts re this, where I try use

var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<AppDBContext>(options => options.UseSqlServer(connection));

I get the error 'IServiceCollection' does not contain a definition for 'AddDbContext'

How do I inject the db connection string , should it be in Startup.cs or should it (can it) be in the DAL project?

TIA

Neal Rogers
  • 498
  • 9
  • 27
  • See the description of the ADO tag, please. – Alexander Petrov Aug 22 '22 at 13:16
  • 1
    All dependency registrations should be done in one place: Composition Root. The DAL project does not need to know anything about the DI-container being used. – Alexander Petrov Aug 22 '22 at 13:19
  • The [AddDbContext](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions.adddbcontext) extension method likely lives in a NuGet package you didn't reference in your project. – Steven Aug 22 '22 at 15:48
  • Ok, great thanks. I had to install (Nuget) the 5.0.17 version of Microsoft.EntityFrameworkCore as opposed to 6.0.8), which does not provide the options.UseSQLServer, so more R&D, but on the right track – Neal Rogers Aug 23 '22 at 06:19

0 Answers0