ASP.NET Core MVC Entity Framework Core: the context type 'DatenbankKontext' only declares a parameterless constructor

asp.net-core entity-framework-core

Question

I have tried to implement Entity Framework Core to my Application I have some trouble. Following error message occurs when I try to debug:

System.ArgumentException: "AddDbContext was called with configuration, but the context type 'DatenbankKontext' only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then 'DatenbankKontext' should declare a constructor that accepts a DbContextOptions and must pass it to the base constructor for DbContext."

That's my DatabaseContext:

using Microsoft.EntityFrameworkCore;

namespace PlaudertischSoftware.Models
{
    public class DatenbankKontext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite(
                @"Server=(localdb)\mssqllocaldb;Database=PlaudertischSoftwareDatenbankCore;Integrated Security=True");
        }
        public virtual DbSet<ObstSpielDaten> ObstSpielDaten { get; set; }
        public virtual DbSet<AutoGaugeDaten> AutoGaugeDaten { get; set; }
    }
}

That's my Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PlaudertischSoftware.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.HttpOverrides;
using System.Net;

namespace PlaudertischSoftware
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            services.AddMvc(option => option.EnableEndpointRouting = false);
            services.AddControllersWithViews()
                .AddJsonOptions(options =>
                {
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
                });
            services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.KnownProxies.Add(IPAddress.Parse("0.0.0.0"));
            });
            services.AddDbContext<DatenbankKontext>(options => {
                options.UseSqlite(Configuration.GetConnectionString("PlaudertischSoftwareDatenbank"));
            });
        }

        [System.Obsolete]
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            app.UseAuthentication();
        }
    }
}

And that's my appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "PlaudertischSoftwareDatenbank": "Server=(localdb)\\mssqllocaldb;Database=PlaudertischSoftwareDatenbank;Integrated Security=True"
  }
}
1
0
3/20/2020 10:37:20 AM

Popular Answer

The error is telling you that you need to add a constructor that takes options.

'DatenbankKontext' should declare a constructor that accepts a DbContextOptions and must pass it to the base constructor for DbContext.

So add one:

public class DatenbankKontext : DbContext
{
    public DatenbankKontext(DbContextOptions options)
        : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite(
            @"Server=(localdb)\mssqllocaldb;Database=PlaudertischSoftwareDatenbankCore;Integrated Security=True");
    }
    public virtual DbSet<ObstSpielDaten> ObstSpielDaten { get; set; }
    public virtual DbSet<AutoGaugeDaten> AutoGaugeDaten { get; set; }
}
1
3/20/2020 10:47:30 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow