i utiliser noyau asp.net de projet, mais quand je veux générer contrôleur avec vue sur un modèle j'ai une erreur:Création d'un contrôleur avec des vues utilisant Asp.net base
Il y a eu une erreur de création du DbContext instance pour obtenir le modèle. Aucun constructeur sans paramètre défini pour cet objet. Constructeur sans paramètre défini pour cet objet. à Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0() à Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute (String [] args ) à Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute (String [ ] args)
c'est le 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; }
}
}
le 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;
}
}
}
Le modèle:
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; }
}
}
Cette erreur quand je veux créer MVC Controller avec des vues pour le modèle utilisateur
Oui, j'essaie d'utiliser les fonctionnalités d'échafaudage de code Visual Studio qui génèrent la classe et les vues automatiquement, c'est pourquoi je ne peux pas créer un contrôleur avec vue, mais je peux créer un contrôleur sans vue Erreur . donc le problème dans la génération de vues. –
Pour ASP.NET Core, vous devez ajouter le générateur de code mvc et les packages d'outils en tant que dépendances pour pouvoir utiliser les fonctions d'échafaudage dans Visual Studio. Edité la réponse pour le même, j'espère que ça aide. –
Je fais référence à [Scaffolding dans ASP.NET Core] (https://code.msdn.microsoft.com/échafaudage-ASPNet-Core-MVC-1e9183fd) pour générer le contrôleur avec vue, mais j'ai eu la même erreur que je avais auparavant. –