1

i use project asp.net core but when i want to generate controller with view for a model i have an error :

There was an error creating the DbContext instance to get the model. No parameterless constructor defined for this object. No parameterless constructor defined for this object. at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0() at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

this is the DBContext :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using WebApplication.Models;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using MySQL.Data.EntityFrameworkCore.Extensions;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;


namespace WebApplication
{
public class HunterViewContext : DbContext
{
    public HunterViewContext(DbContextOptions<HunterViewContext> options)
    : base(options)
    { }
    public DbSet<User> User{ get; set; }       
}
}

The DBContextFactory:

using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication.Models
{
public static class HunterViewContextFactory
{
    public static HunterViewContext Create(string connectionString)
    {
        var optionsBuilder = new DbContextOptionsBuilder<HunterViewContext>   ();
        optionsBuilder.UseMySQL(connectionString);

        //Ensure database creation
        var context = new HunterViewContext(optionsBuilder.Options);
        context.Database.EnsureCreated();

        return context;
    }
    }
    }

The Model :

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;

namespace WebApplication.Models
{
public class User 
{

    public User()
    {

    }
        public int Id { get; set; }

        [MaxLength(30)]
        public string Name { get; set; }

        [MaxLength(50)]
        public string LastName { get; set; }
    }

    }

This the error when i want to create MVC Controller With Views for Model User

error

1 Answer 1

1

Are you trying to use Visual Studio code scaffolding features which generates the controller class and views automatically?. If so, it is not yet supported for ASP.Net core project templates. You need to create the views and controller classes by yourself, refer to the documentation Add Controllers

Open project.json file and add the below package dependencies and tools

In Dependency section

Add the below packages

//Code Generators Package Generate Controller,Views

"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final", 
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final"

In Tools section

Add the below tool

//Access Command Tools Code Generation

"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final"

Refer to Scaffolding in ASP.NET Core for more details.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes , i try to use Visual Studio code scaffolding features which generates the controller class and views automatically, so that's why i can't create a controller with view , but i can create a controller without views and i don't any error . so the problem in the generation of views.
For ASP.NET Core, you need to add mvc code generator and tools packages as dependencies to be able to use the scaffolding features in visual studio. Edited the answer for the same, hope it helps.
I refer Scaffolding in ASP.NET Core to generate the controller with view but i had the same error that i haved before .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.