2017-09-23 1 views
-1

.Net Core의 첫 번째 응용 프로그램을 만들고 있습니다.필수 매개 변수 'options'에 해당하는 인수가 없습니다.

나는 어떤 이유로이 빌드 오류를 받고 있어요 :

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

내가 구글 검색이나 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); 
     } 
    } 
} 

컨트롤러 클래스 :

(굵은 텍스트가 같은 오류와 함께 Visual Studio에서 밑줄
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); 
     } 
    } 
} 

"개인 LakeViewContext의 dB = 새로운 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(); 
     } 
    } 
} 

답변

2

LakeViewContextDbContextOptions<LakeViewContext>를 기대 즉

public CoursesController(LakeViewContext dbContext) 
{ 

    db = dbContext; 

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

의존성 주입이 당신에게 그것을 전달합니다, 컨트롤러에 생성자를 추가하고 주입 얻을 것이다 귀하의 생성자에 dbContext을 추가해야합니다 생성자로 전달됩니다. 그러나, 당신은 아무것도를 제공하지 않고 생성자를 호출 :

private LakeViewContext db = new LakeViewContext(); 

이 문제를 해결하려면, 당신은 당신이 설정 한 의존성 삽입 (Dependency Injection) 시스템에 연결할 수 있습니다. - 그냥 그렇게 사용

public class CoursesController : Controller 
{ 
    private readonly LakeViewContext db; 

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

    ... 

은 ASP.NET 코어 의존성 삽입 (Dependency Injection) 시스템은 생성자에서 LakeViewContext를 제공합니다 다음과 같이이 작업을 수행하려면 컨트롤러를 변경합니다.

0

옵션을 전달하지 않고 컨트롤러에서 dbcontext를 새로 작성하려고합니다.

대신

관련 문제