Error When Adding Controller in ASP.NET MVC

2

Good morning, I'm making a small solution using ASP.NET MVC with Entity Framework 6 to attend a work from my course. I created the context class:

namespace WillianRibeiro.EstudandoMVC.Web.Data{
public class EstudandoMVCContext : DbContext
{
    public EstudandoMVCContext()
        :base("EstudandoMVC_Desenv")
    {
    }
    public DbSet<Usuario> Usuario { get; set; }
 }  }

I created a Model:

namespace WillianRibeiro.EstudandoMVC.Web.Data{
public class EstudandoMVCContext : DbContext
{
    public EstudandoMVCContext()
        :base("EstudandoMVC_Desenv")
    {

    }

    public DbSet<Usuario> Usuario { get; set; }

 }}

I configured web config:

    <connectionString>
<add name="EstudandoMVC_Desenv" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\OneDrive\Projetos ASP NET\COPEL\WillianRibeiro.EstudandoMVC\WillianRibeiro.EstudandoMVC.Web\App_Data\EstudandoMVC_BD.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

The problem occurs when I add the controller "User" I am trying to add the MVC 5 controller to the views and using the Entity Framework. I make all the configuration and when I command create of the error: "An error occurred while executing the selected code generator: An exception was thrown by the destination of a call."

I have reviewed the code several times and can not resolve it.

    
asked by anonymous 10.08.2017 / 16:42

2 answers

-1

You need to create a Class context and the User Class (which refers to your Table), would look like this:

This is the Context class, where you reference your tables

public partial class EstudandoMVCContext: DbContext
{
    public EstudandoMVCContext()
        : base("name=EstudandoMVC_Desenv")//Refere ao nome da sua conexão
    {
    }

    public virtual DbSet<Usuario> Usuario { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Usuario>()
            .Property(e => e.nomeUsuario)
            .IsUnicode(false);

    }
}

And here is going to be your User class (representing your table)

[Table("Usuario")]
public partial class Usuario
{
    [Key]
    public int idUsario { get; set; }

    [StringLength(50)]
    public string nomeUsuario { get; set; }
}

If you already have the tables made in the database, it is easier to let the Entity Framework itself generate those classes.

    
11.01.2018 / 21:05
-1

Making a connection in Visual Studio 2017 - ASP NET MVC - MySQL 5.5 was having the same problem reported by his colleague.

Error creating Controller:

  

"Error while executing selected code generator: An exception was thrown   triggered by the destination of a call "

Following the guidelines above setting the "Context", I was able to solve the problem.

Thank you.

    
28.09.2018 / 13:42