Skip to content

EF Migration it works only to transient scope #28

@DSBRBrasil

Description

@DSBRBrasil

Hi guys !!! Why doesn't works when singleton or scoped scope are used ?
var connectionString = "server=localhost\\sqlexpress;initial catalog=db_sgone_desktop;user id=sa;password=123456;"; services.AddDbContext<ErpDbContext>(opt => opt.UseSqlServer(connectionString)); services.AddSingleton<ErpDbContext>(); services.AddSingleton<IVersaoSistemaRepository, VersaoSistemaRepository>(); services.AddSingleton<IVersionamentoRepository, VersionamentoRepository>();
now, I write the command
add-migration initial, and after update-database
I get the following error:
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Dsbr.Source.Domain.Interfaces.Repositories.INumeroNotaSerieRepository Lifetime: Singleton ImplementationType: Dsbr.Source.Infra.Repositories.NumeroNotaSerieRepository': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Dsbr.Source.Infra.EF.Context.ErpDbContext]' from singleton 'Dsbr.Source.Domain.Interfaces.Repositories.INumeroNotaSerieRepository'.)

Therefore, when i change the scope to services.AddTransient it works fine ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions