kianoush
kianoush

Reputation: 21

No parameterless constructor was found on 'ApplicationDbContext'. in Asp Core

i create this for Initial Database in ASP Core . i using this :

dotnet ef migrations add InitialCreate

but it show me this error :

No parameterless constructor was found on 'ApplicationDbContext'. Either add a parameterless constructor to 'ApplicationDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationDbContext'.

but i created a Constuctor . whats the problem ? how can i solve this problem ?

ApplicationDBContext:

 public class ApplicationDbContext : ApplicationDbContextBase
{
    public ApplicationDbContext(
        IOptionsSnapshot<SiteSettings> siteSettings
        , IHttpContextAccessor httpContextAccessor
        , IHostingEnvironment hostingEnvironment
        , ILogger<ApplicationDbContextBase> logger)
        : base(siteSettings, httpContextAccessor, hostingEnvironment, logger)
    {
    }


    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.AddCustomIdentityMappings(SiteSettings.Value);
        builder.AddAuditableShadowProperties();
    }

ApplicationDbContextBase:

  public abstract class ApplicationDbContextBase :
   IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>,
   IUnitOfWork
{
    protected readonly IHostingEnvironment HostingEnvironment;
    protected readonly IHttpContextAccessor HttpContextAccessor;
    protected readonly ILogger<ApplicationDbContextBase> Logger;
    protected readonly IOptionsSnapshot<SiteSettings> SiteSettings;
    private string v;

    public ApplicationDbContextBase(string v)
    {
        this.v = v;
    }

    protected ApplicationDbContextBase(
        IOptionsSnapshot<SiteSettings> siteSettings,
        IHttpContextAccessor httpContextAccessor,
        IHostingEnvironment hostingEnvironment,
        ILogger<ApplicationDbContextBase> logger)
    {
        SiteSettings = siteSettings;
        SiteSettings.CheckArgumentIsNull(nameof(SiteSettings));

        HttpContextAccessor = httpContextAccessor;
        HttpContextAccessor.CheckArgumentIsNull(nameof(HttpContextAccessor));

        HostingEnvironment = hostingEnvironment;
        HostingEnvironment.CheckArgumentIsNull(nameof(HostingEnvironment));

        Logger = logger;
        Logger.CheckArgumentIsNull(nameof(Logger));
    }

Update

I upload the my Project in Github . GitHub

Upvotes: 1

Views: 1224

Answers (3)

Pouya Samie
Pouya Samie

Reputation: 3723

The problem is because you don't have any default constructor in your ApplicationDbContextBase class and the ApplicationDbContextBase can not be resolved by Dependency injection(see this url for more information). change

   public ApplicationDbContextBase(string v)
    {
        this.v = v;
    }

to

  public ApplicationDbContextBase()
    {

    }

or add the default constructor.

Upvotes: 1

VahidN
VahidN

Reputation: 19156

  • When the EF Core wants to instantiate the context, it will use the built-in dependency injection system to do it. Now it will find that you have more than one constructor here. Then it will throw an exception to tell you: I don't know which one of these constructors should be used to instantiate the context. To solve it, remove all of the other constructors (you should not have more than 1 constructor here). Just keep the one with IOptionsSnapshot<SiteSettings> parameter and remove the others.
  • EF Core is able to handle all of the injected constructor parameters here using the built-in dependency injection system. But you should have only 1 constructor to make it work. It doesn't use the raw new ApplicationDbContext(). It will use serviceScope.ServiceProvider.GetRequiredService behind the scene to instantiate the context and all of its injected parameters.
  • If you need other config params here, just use the injected IOptionsSnapshot<SiteSettings> and modify the SiteSettings, but don't add other constructors for that.
  • If you have moved your context to another assembly, EF Core should be able to find the injected dependencies and you should provide the --startup-project ../your_project_name/ too.

Upvotes: 3

umasankar
umasankar

Reputation: 607

public class ApplicationDbContext : ApplicationDbContextBase
{
    public ApplicationDbContext(
        IOptionsSnapshot<SiteSettings> siteSettings
        , IHttpContextAccessor httpContextAccessor
        , IHostingEnvironment hostingEnvironment
        , ILogger<ApplicationDbContextBase> logger)
        : base(siteSettings, httpContextAccessor, hostingEnvironment, logger)
    {
}

In this base keyword in used for send the data ApplicationDbContextBase Not ApplicationDbContext.

the ApplicationDbContext and ApplicationDbContext class need to pass mulitple paramters.so, the class say No parameterless constructor was found on 'ApplicationDbContext'. If you need to pass parameter. otherwise create constructor without parameter. Like

public class ApplicationDbContext : ApplicationDbContextBase
{
public ApplicationDbContext() 
 {
 }
}

and pass base parameter.

If you are Using in DI register all required Objects. I Think It's Helpful for Identify and solve problem.

Upvotes: 0

Related Questions