Reputation: 11
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
Upvotes: 1
Views: 3490
Reputation: 1349
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.
Upvotes: 1