Error in asp.net mvc core 2 scaffolding generated Controller

Bob5421

I am working on an ASP.Net Core 2 MVC Application.

I am generating one controller per table for my backoffice with this command:

dotnet aspnet-codegenerator --project . controller -name MYTABLEController -m MYTABLE -dc MYDBContext

(replace MYTABLE and MYDBContext are examples).

Here is what the csharp Controller looks like:

namespace MYPROJECT.Controllers
{
    public class MYTABLEController : Controller
    {
        private readonly MYDBContext _context;

        public ContactsController(MYDBContext context)
        {
            _context = context;
        }

Has you can see it creates a constructor which accepts one parameter: the database object (entityframework).

When i call the web page, i get an error on this context because it is not initialized.

Here what i have to do to make it work:

namespace MYPROJECT.Controllers
{
    public class MYTABLEController : Controller
    {
        private readonly MYDBContext _context = new MYDBContext();

        public ContactsController()
        {

        }

So my question are:

  1. Is there a best way to correct this problem ? I am wondering if it is really a bug or if i miss something.

  2. Look and feel of each generated chtml view is very ugly. Are there some options to get something more sexy ?

Thanks

Muqeet Khan

The generated controller code seems fine. You did not mention the exact error, but, I am assuming you are getting the error because you haven't added the MYDBContext in your services container.

In your startup.cs file you need to add this MYDBContext into the services container so that your controller will get it injected properly. Example,

    services.AddDbContext<MYDBContext>(options =>
        options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

After doing this, if you get the error as below:

System.ArgumentException: AddDbContext was called with configuration, but the context type 'MyDBContext' only declares a parameterless constructor.

then it means your scaffolded DbContext is not configured properly to be injected with the correct options parameter. Update the constructor to:

public partial class MYDBContext : DbContext
{
    public MYDBContext(DbContextOptions<MYDBContext> options)
        : base(options)
    {

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Problem scaffolding new controller using Entity Framework Core in ASP.NET Core 2.1 MVC

Using ASP.net MVC 5 scaffolding in ASP.net Core MVC 2

SOLVED - How can I use scaffolding in ASP.NET Core MVC ? I get an error

Sessionless controller in Asp.Net Mvc Core

ASP.NET Core MVC Subaction in a controller

Passing dynamically generated textbox values to controller in ASP.NET MVC

ASP.NET Core MVC - app.UseStatusCodePagesWithReExecute("/Error/{0}") always shows status code of 0 in controller

When I scaffold a controller in Asp.net Core (MVC), I get a Code generation error

Change controller route name in ASP.NET Core MVC 404 error

Error when scaffolding using Entity Framework Core in ASP.Net core

Asp.Net Core MVC Movie App - Scaffolding Item not working properly

How set a identity scaffolding item/page how initial page in asp.net MVC core?

Problem running project after Identity scaffolding in ASP.NET Core MVC project on Linux

Problem with Foreign keys displaying on asp.net mvc core index pages after scaffolding

Getting a 2D array or Json strip from a page to a controller asp.net core mvc

displaying json strings from controller in view over ajax in asp.net core 2 MVC

Asp.Net Core 2.0 error while scaffolding my database to generate Models

How to navigate to an ASP.NET Core MVC controller in Blazor app?

Reading information from database by controller in ASP.NET Core MVC

Set ViewBag property in the constructor of a ASP.NET MVC Core controller

View data from another controller in ASP.NET Core MVC

How to pass two parameters to a controller in ASP.NET Core MVC?

Weird behaviour with controller name in ASP.NET Core 6.0 MVC

Post form to controller in ASP.NET Core MVC not working

ASP .NET MVC Core + SignalR: Redirecting from a Hub to a Controller

ASP.NET Core MVC local database and model controller

Sending JSON string to ASP.NET Core MVC Controller

call another controller in different project on asp.net mvc core

Controller action alias in ASP.NET MVC CORE