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
