Reputation: 21
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
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
Reputation: 19156
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.new ApplicationDbContext()
. It will use serviceScope.ServiceProvider.GetRequiredService
behind the scene to instantiate the context and all of its injected parameters.IOptionsSnapshot<SiteSettings>
and modify the SiteSettings
, but don't add other constructors for that.--startup-project ../your_project_name/
too.Upvotes: 3
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