locked
database error when scaffolding RRS feed

  • Question

  • User456628452 posted

    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

      public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<IntranetContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
                services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<ApplicationDbContext>();
                services.AddControllersWithViews();
                services.AddRazorPages();
            }

    appsettings.json

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "ConnectionString": {
        "DevConnection": "Server= mypc;Database:ERP;Trusted_Connection=True;MultipleActiveResultSets=true"
      }
    }
    

    context file

    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; }
            
        }
    }
    

    Saturday, February 29, 2020 10:34 AM

All replies

  • User1120430333 posted

    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>

    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) { }

    .

    Saturday, February 29, 2020 12:55 PM
  • User-854763662 posted

    Hi marya ,

    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; }
            
    }

    Reference: https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

    Best Regards,

    Sherry

    Thursday, March 5, 2020 9:35 AM