Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

dbContext Error

$
0
0

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 { getset; }    }
}

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 { getset; }        [Display(Name = "Is Active")]        public bool IsActive { getset; }        public int Level { getset; }        public int Strength { getset; }        public int Dexterity { getset; }        public int Intelligence { getset; }        public List<Equipment> Equipment { getset; }    }
}

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. 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>