2017-09-23 62 views
-1

我正在使用.Net核心中的第一個應用程序。沒有任何參數對應於所需的形式參數「選項」

我得到這個生成錯誤因爲某些原因:

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

控制器類:

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 =新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

LakeViewContext需要一個DbContextOptions<LakeViewContext>被傳遞給它的構造函數。但是,您呼叫的構造不提供任何內容:

private LakeViewContext db = new LakeViewContext(); 

爲了解決這個問題,你可以插入到您所設置的依賴注入系統。 - 只需使用

public class CoursesController : Controller 
{ 
    private readonly LakeViewContext db; 

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

    ... 

的ASP.NET核心依賴注入系統將爲您提供在構造一個LakeViewContext:要做到這一點,如下更改您的控制器。

0

您正試圖在控制器中更新dbcontext而不傳入選項。

而應該添加一個構造函數來你的控制器,並添加的DbContext到您的構造函數,因此它會被注入,即

public CoursesController(LakeViewContext dbContext) 
{ 

    db = dbContext; 

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

依賴注入將它傳遞到你

相關問題