Mel Mason
Mel Mason

Reputation: 1

Receiving "InvalidOperationException:" in Asp.net MVC

I am trying to create a simple Asp.NET MVC database where a user can create an account, create categories for recipes, and then enter their recipe and file them into the category of their choosing. However, when I attempt to run the test to see if I can reach my list of categories(where I can also add a category), I get the following error message:

InvalidOperationException: Unable to resolve service for type 'LC101Project2017.Data.RecipeDbContext' while attempting to activate 'LC101Project2017.Controllers.CategoryController'.

I'm new to C# and am completely confused as to what I'm doing wrong. Here are my codes:

Controller: (CategoryController.cs)

public class CategoryController : Controller
{
    private readonly RecipeDbContext context;

    public CategoryController(RecipeDbContext dbContext)
    {
        context = dbContext;
    }

    public IActionResult Index()
    {
        List<RecipeCategory> categories = context.Categories.ToList();

        return View(categories);
    }

    public IActionResult Add()
    {
        AddCategoryViewModel addCategoryViewModel = new AddCategoryViewModel();

        return View(addCategoryViewModel);
    }

    [HttpPost]
    public IActionResult Add(AddCategoryViewModel addCategoryViewModel)
    {
        if (ModelState.IsValid)
        {
            RecipeCategory newCategory = new RecipeCategory
            {
                Name = addCategoryViewModel.Name
            };

            context.Categories.Add(newCategory);
            context.SaveChanges();

            CategoryController:  return Redirect("/Category");
        };
        return View(addCategoryViewModel);
    }
}

Database (RecipeDbContext.cs)

public class RecipeDbContext : DbContext
{
    public DbSet<Recipe> Recipes { get; set; }
    public DbSet<RecipeCategory> Categories { get; set; }
}

MODEL (RecipeCategory.cs)

public class RecipeCategory
{
    public int ID { get; set; }
    public string Name { get; set; }

    public IList<RecipeCategory> RecipeCategories { get; set; }
}

Upvotes: 0

Views: 357

Answers (1)

Mike Mat
Mike Mat

Reputation: 692

Check if you have configured to use your DbContext, RecipeDbContext, inside ConfigureServices method in Startup.cs

The method should look like this;

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddDbContext<RecipeDbContext >(options =>
                options.UseSqlServer(Configuration.GetConnectionString("ConnectionStringName")));
    }

Updated the answer in response to the comment by @Mel Mason

You need to declare a constructor that accepts DbContextOptions<RecipeDbContext>.

    public class RecipeDbContext : DbContext
    {
        public DbSet<Recipe> Recipes { get; set; }
        public DbSet<RecipeCategory> Categories { get; set; }

        public RecipeDbContext(DbContextOptions<RecipeDbContext> options)
            : base(options)
        { }
    }

You can also check the official documentation:

https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

Hope this helps.

Upvotes: 2

Related Questions