i am having error when i tries to scaffold a controller "No database provide rhas been configured for this dbcontext. a provider can be configured by overriding the db context.On Configuring method or by using Adddbcontext on the applocation service provider.if
adddbcontext is used, then also ensure that your dbcontext type accpets a dbcontextoptions<TContext? object in its constructor and passes it to the base constructor for DbContext".
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Intranet
{
public class IntranetContext : DbContext
{
//public IntranetContext(DbContextOptions<IntranetContext> options) : base(options)
//{
//}
public IntranetContext()
// C# will call base class parameterless constructor by default
{
}
public DbSet<GenerateDocumentNumber> GenerateDocumentNumber { get; set; }
}
}
If you are using 'options' for the connectionstring, then you are implying that the service, an IoC, will instance the object allowing dependency injection of the connectionstring object into the Dbcontext class/object, which you have commented out.
The DbContextOptions can be supplied to the DbContext by overriding the OnConfiguring method or externally via a constructor argument.
If both are used, OnConfiguring is applied last and can overwrite options supplied to the constructor argument.
<end>
in startup.cs
services.AddDbContext<PublishingCompanyContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
the context....
public partial class PublishingCompanyContext : DbContext
{
public PublishingCompanyContext(DbContextOptions<PublishingCompanyContext> options)
: base(options)
{
}
public virtual DbSet<Article> Article { get; set; }
public virtual DbSet<Author> Author { get; set; }
public virtual DbSet<Payroll> Payroll { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
.
If you find the post has answered your issue, then please mark post as 'answered'.
No database provide rhas been configured for this dbcontext. a provider can be configured by overriding the db context.On Configuring method or by using Adddbcontext on the applocation service provider.if adddbcontext is used, then also ensure that your dbcontext
type accpets a dbcontextoptions<TContext? object in its constructor and passes it to the base constructor for DbContext
The error message says your DbContext( IntranetContext ) needs a constructor which accepts a DbContextOptions. So adding below constructor probably solves your problem.
public class IntranetContext : DbContext
{
public IntranetContext(DbContextOptions<IntranetContext> options) : base(options)
{
}
public DbSet<GenerateDocumentNumber> GenerateDocumentNumber { get; set; }
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
16 Points
18 Posts
database error when scaffolding
Feb 29, 2020 10:34 AM|marya|LINK
i am having error when i tries to scaffold a controller "No database provide rhas been configured for this dbcontext. a provider can be configured by overriding the db context.On Configuring method or by using Adddbcontext on the applocation service provider.if adddbcontext is used, then also ensure that your dbcontext type accpets a dbcontextoptions<TContext? object in its constructor and passes it to the base constructor for DbContext".
Here is what i haved done so far
startup.cs
appsettings.json
context file
Contributor
4923 Points
4200 Posts
Re: database error when scaffolding
Feb 29, 2020 12:55 PM|DA924|LINK
If you are using 'options' for the connectionstring, then you are implying that the service, an IoC, will instance the object allowing dependency injection of the connectionstring object into the Dbcontext class/object, which you have commented out.
https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
<copied>
The DbContextOptions can be supplied to the DbContext by overriding the OnConfiguring method or externally via a constructor argument.
If both are used, OnConfiguring is applied last and can overwrite options supplied to the constructor argument.
<end>
.
Contributor
2070 Points
606 Posts
Re: database error when scaffolding
Mar 05, 2020 09:35 AM|Sherry Chen|LINK
Hi marya ,
The error message says your DbContext( IntranetContext ) needs a constructor which accepts a DbContextOptions. So adding below constructor probably solves your problem.
Reference: https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
Best Regards,
Sherry
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.