rhslogic
rhslogic

Reputation: 359

There is no argument given that corresponds to the required formal parameter 'options'

I'm working on my first application in .Net Core.

I'm getting this build error for some reason:

Error CS7036 There is no argument given that corresponds to the required formal parameter 'options' of 'LakeViewContext.LakeViewContext(DbContextOptions<LakeViewContext>)' LakeView

I wasn't able to find a solution through Google Search or MS documentation.

My Context class:

using LakeView.Models;
using Microsoft.EntityFrameworkCore;

namespace LakeView
{
    public class LakeViewContext : DbContext
    {
        public LakeViewContext(DbContextOptions<LakeViewContext> options) : base(options)
        {

        }

        public DbSet<HTMLElement> HTMLElements { get; set; }
        public DbSet<CustomizedElement> CustomizedElements { get; set; }
        public DbSet<TemplateFileType> TemplateFileTypes { get; set; }
        public DbSet<StyleFile> StyleFiles { get; set; }
        public DbSet<Template> Templates { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Page> Pages { get; set; }
        public DbSet<HTML> HTMLs { get; set; }
        public DbSet<Comment> Comments { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<CustomizedElementTemplate>()
                .HasKey(s => new { s.CustomizedElementId, s.TemplateId });
            base.OnModelCreating(modelBuilder); 
        }
    }
}

Controller class:

using LakeView.Models;
using LakeView.Models.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace LakeView.Controllers
{
    public class CoursesController : Controller
    {

        private LakeViewContext db = new LakeViewContext();

        public IActionResult Index()
        {
            ICollection<Course> courses = db.Courses.ToList();
            return View(courses);
        }

        [HttpGet]
        public IActionResult CreateCourse()
        {
            return View("CreateCourse");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult CreateCourse(CreateCourseViewModel courseVM)
        {
            if (ModelState.IsValid)
            {
                Course newCourse = new Course()
                {
                    CourseCode = courseVM.CourseCode,
                    CourseTitle = courseVM.CourseTitle,
                    MasterOU = int.Parse(courseVM.MasterOU)
                };

                db.Courses.Add(newCourse);
                db.SaveChanges();

                return RedirectToAction("Index");

            }
                return View("CreateCourse", courseVM);
        }
    }
}

(bold text is underlined in Visual Studio with the same error

"private LakeViewContext db = new LakeViewContext();"

Startup class:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using LakeView.Models;

namespace LakeView
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            var connection = @"Data Source = (localdb)\MSSQLLocalDB; Database = LakeViewData; Trusted_Connection = True;";
            services.AddDbContext<LakeViewContext>(options => options.UseSqlServer(connection));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            //loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();

            app.UseMvcWithDefaultRoute();
        }
    }
}

Upvotes: 9

Views: 24499

Answers (3)

Ian Gibblet
Ian Gibblet

Reputation: 582

When observing at how the classes are generated via the database first approach, I was able to fix this error with an empty constructor.

    public class MyDBContext : DbContext
    {
        public MyDBContext()
        {
        }
    //Rest of your code
}

Upvotes: 1

Kirk Larkin
Kirk Larkin

Reputation: 93003

LakeViewContext expects a DbContextOptions<LakeViewContext> to be passed into its constructor. However, you are calling the constructor without providing anything:

private LakeViewContext db = new LakeViewContext();

To fix the issue, you can just plug into the Dependency Injection system that you've set up. To do this, change your controller as follows:

public class CoursesController : Controller
{
    private readonly LakeViewContext db;

    public CoursesController(LakeVieContext db)
    {
        this.db = db;
    }

    ...

The ASP.NET Core Dependency Injection system will provide you with a LakeViewContext in the constructor - Just use that.

Upvotes: 16

Joe Audette
Joe Audette

Reputation: 36696

you are trying to new up the dbcontext in your controller without passing in the options.

You should instead add a constructor to your controller and add the dbContext to your constructor so it will get injected, ie

public CoursesController(LakeViewContext dbContext)
{

   db = dbContext;

}
private LakeViewContext db;
... the rest of your code

dependency injection will pass it in to you

Upvotes: 0

Related Questions