我为ASP Core中的初始数据库创建了这个。我用这个:
dotnet ef migrations add InitialCreate
但是它显示了这个错误:
在' ApplicationDbContext'上找不到无参数构造函数。将无参数构造函数添加到' ApplicationDbContext'或者添加' IDbContextFactory'的实现在与#D; ApplicationDbContext'。
相同的程序集中
但我创建了一个Constuctor。有什么问题 ?我该如何解决这个问题?
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));
}
更新
我在Github上传了我的Project。 GitHub
答案 0 :(得分:3)
I don't know which one of these constructors should be used to instantiate the context
。要解决它,请删除所有其他构造函数(此处不应包含 1 构造函数)。只需保留一个IOptionsSnapshot<SiteSettings>
参数并删除其他参数即可。new ApplicationDbContext()
。它将在场景后面使用serviceScope.ServiceProvider.GetRequiredService
来实例化上下文及其所有注入的参数。IOptionsSnapshot<SiteSettings>
并修改SiteSettings
,但不要为此添加其他构造函数。--startup-project ../your_project_name/
。答案 1 :(得分:1)
问题是因为您的ApplicationDbContextBase类中没有任何默认构造函数,并且依赖注入无法解析ApplicationDbContextBase(有关详细信息,请参阅此url)。改变
public ApplicationDbContextBase(string v)
{
this.v = v;
}
到
public ApplicationDbContextBase()
{
}
或添加默认构造函数。
答案 2 :(得分:0)
public class ApplicationDbContext : ApplicationDbContextBase
{
public ApplicationDbContext(
IOptionsSnapshot<SiteSettings> siteSettings
, IHttpContextAccessor httpContextAccessor
, IHostingEnvironment hostingEnvironment
, ILogger<ApplicationDbContextBase> logger)
: base(siteSettings, httpContextAccessor, hostingEnvironment, logger)
{
}
在此base关键字中用于发送数据ApplicationDbContextBase Not ApplicationDbContext。
ApplicationDbContext和ApplicationDbContext类需要传递多个paramters.so,该类说“在ApplicationDbContext”上找不到无参数构造函数。如果需要传递参数。否则创建没有参数的构造函数。 像
public class ApplicationDbContext : ApplicationDbContextBase
{
public ApplicationDbContext()
{
}
}
并传递base参数。
如果您在DI中使用所有必需的对象。 我认为它有助于识别和解决问题。