I am currently learning bits and pieces with ASP.NET Core. But I am struggling to get to the bottom of an error I am receiving and feel I may have missed something, but do not know what.
This error appears when I try to load the Index view.
At the top of the controller class I have:
private readonly ApplicationDbContext _context;
And my Index Action is:
public IActionResult Index() { var model = _context.Characters.ToList(); return View(model); }
And the context is found in my model folder called ApplicationDbContext.cs, which looks like this:
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.EntityFrameworkCore;namespace ForgingAhead.Models { public class ApplicationDbContext : DbContext { public DbSet<Character> Characters { get; set; } } }
Which is referencing the model here:
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.ComponentModel.DataAnnotations;namespace ForgingAhead.Models { public class Character { public string Name { get; set; } [Display(Name = "Is Active")] public bool IsActive { get; set; } public int Level { get; set; } public int Strength { get; set; } public int Dexterity { get; set; } public int Intelligence { get; set; } public List<Equipment> Equipment { get; set; } } }
And finally in my Startup.cs, my configuration:
public void ConfigureServices(IServiceCollection services) { services.AddEntityFrameworkSqlite() .AddDbContext<ApplicationDbContext>(); services.AddMvc(); }
As far as I can see, I am doing what the error is telling me to do and that's configuring a service and calling AddDbContext. I have been following a tutorial to do this and last time I touch ASP.NET was 3 or 4 years ago, hence I am trying to get back into the flow of learning it properly. But for some reason, it doesn't seem obvious to me why this is not working.