C# Dependency injection with partial dbContext classes

45 Views Asked by At

I have an Entity Framework 5 model, where my DbContext is split into two partial files. The services.AddDbContext() works and it is injected into the controllers. The problem arises when I want to inject it into my services. They are added using services.AddScoped(), but I get the following error:

Unable to activate type 'MyDbContext'. The following constructors are ambiguous....

Can't find anything about it. Any help?

Edit:

One partial class/file does not have any constructor. The other one has two, auto generated by the scaffold:

public MyDbContext(){}

public MyDbContext(DbContextOptions<MyDbContext> options) : base(options){}

I must confess, don't know which one is really needed...

1

There are 1 best solutions below

0
Emaborsa On BEST ANSWER

It was my mistake, I did not notice the second constructor in MyService. Removing one worked.