Error adding mvc controller using the Entity framework

Asked

Viewed 52 times

0

I’m trying to create a MVC application using Entity Framework Core, and Asp.Net Core MVC 5, using Mysql through Pomelo.EntityFrameworkCore.Mysql, the domain class is defined as:

public class Aluno
{
    [Key]
    public int AlunoId { get; set; }
    public string Nome { get; set; }
    [DisplayFormat(DataFormatString ="{0:dd/MM/yyyy", ApplyFormatInEditMode = true)]
    public DateTime DataNascimento { get; set; }
}

My Dbcontext is set to:

 public class EscolaDbContexto: DbContext
{
    

    public EscolaDbContexto(DbContextOptions options):base(options)
    {
        

    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        List<Aluno> seed = new List<Aluno>()
        {
            new Aluno(){Nome = "Sebastião", DataNascimento = Convert.ToDateTime("31/07/1984")},
            new Aluno(){Nome = "José", DataNascimento = Convert.ToDateTime("01/03/1985") },
            new Aluno(){Nome = "Pedro", DataNascimento = Convert.ToDateTime("01/07/1987") }


        };
        modelBuilder.Entity<Aluno>().HasData(seed);
    }
    public DbSet<Aluno> Albuns { get; set; }
    
}

And my appsetings.json is defined as:

In addition to setting up in Startup.Cs as follows

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddDbContext<Models.Contexto
            .EscolaDbContexto>(options => options
            .UseMySql(Configuration.GetConnectionString("EscolaDbContext")));
    }

However, when I try to create a controller using the code generator "MVC Controller with views, using the Entity Framework, selecting the Student class, as domain and Escoladbcontext as data context, the following error appears:

{
"Logging":{
    "LogLevel":{
        "Default": "Information",
        "Microsoft":"Warning",
        "Microsoft.Hosting.Lifetime": "Information"
    }
},
"ConnectionStrings":{
    "EscolaDbContext":"server=127.0.0.1; port=3306;database=escolaDb;uid=root;password=1234"
},
"AllowedHosts":"*"

}

inserir a descrição da imagem aqui

What I’m doing wrong?

[Edit]In scaffolding, it says "MVC Controller with views, using the Entity Framework", and I’m using the Entity Framework Core. Would this code generator not be incompatible with that version?

  • always has the possibility to create the empty controller and put the code manually, there can test its implementation and see if there is no error of code or configuration, the scaffolding sometimes gives error and does not show what is

  • It should not be influencing, but there is an misspelling here "public Dbset<Student> Albuns { get; set; } ". Already tried running EF Scaffolding via command line?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.