【问题标题】:There is no argument given that corresponds to the required formal parameter 'options'没有给出与所需形式参数“选项”相对应的参数
【发布时间】:2017-09-23 21:08:47
【问题描述】:

我正在 .Net Core 中开发我的第一个应用程序。

由于某种原因,我收到此构建错误:

错误 CS7036 没有给出与 'LakeViewContext.LakeViewContext(DbContextOptions)' LakeView 所需的形参 'options' 对应的参数

我无法通过 Google 搜索或 MS 文档找到解决方案。

我的上下文类:

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

控制器类:

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

(粗体文字在 Visual Studio 中加下划线,错误相同

"私有 LakeViewContext db = new LakeViewContext();"

启动类:

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

【问题讨论】:

    标签: c# asp.net asp.net-core asp.net-core-mvc entity-framework-core


    【解决方案1】:

    LakeViewContext 期望将 DbContextOptions&lt;LakeViewContext&gt; 传递到其构造函数中。但是,您在没有提供任何内容的情况下调用构造函数:

    private LakeViewContext db = new LakeViewContext();
    

    要解决此问题,您只需插入已设置的依赖注入系统即可。为此,请按如下方式更改您的控制器:

    public class CoursesController : Controller
    {
        private readonly LakeViewContext db;
    
        public CoursesController(LakeVieContext db)
        {
            this.db = db;
        }
    
        ...
    

    ASP.NET Core 依赖注入系统将在构造函数中为您提供LakeViewContext - 只需使用它即可。

    【讨论】:

      【解决方案2】:

      您正在尝试在控制器中新建 dbcontext 而不传递选项。

      您应该改为向控制器添加一个构造函数,并将 dbContext 添加到您的构造函数中,这样它就会被注入,即

      public CoursesController(LakeViewContext dbContext)
      {
      
         db = dbContext;
      
      }
      private LakeViewContext db;
      ... the rest of your code
      

      依赖注入会传递给你

      【讨论】:

        【解决方案3】:

        在观察如何通过数据库优先方法生成类时,我能够使用空构造函数修复此错误。

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

        }

        【讨论】:

          猜你喜欢
          • 2016-05-13
          • 1970-01-01
          • 1970-01-01
          • 2016-11-11
          • 2019-08-09
          • 2017-07-18
          • 2022-08-24
          • 2019-01-16
          • 1970-01-01
          相关资源
          最近更新 更多