Error when adding Controler to ASP.NET MVC

Asked

Viewed 3,104 times

2

Good morning, I am making a small solution using ASP.NET MVC with Entity Framework 6 to meet a work of my course. I believe 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 the Model:

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

    }

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

 }}

I set up the 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 "User" controller I am trying to add the MVC 5 controller with the views and using the Entity Framework. I do all the configuration and when I have created the error: "Error executing selected code generator: An exception was triggered by the destination of a call".

I’ve gone over the code several times and I can’t solve it.

3 answers

0

In startup.Cs:

`public void ConfigureServices(IServiceCollection services)
        {
            var connection = Configuration["ConexaoMySql:MySqlConnectionString"];
            services.AddDbContext<---SeuContexto--->(options =>
                options.UseMySql(connection)
            );
            // Add framework services.
            services.AddMvc();
        }`

-1

Connecting to Visual Studio 2017 - ASP NET MVC - Mysql 5.5 was having the same problem reported by the colleague.

Error while creating Controller:

"Error running selected code generator: An exception was triggered by a call destination"

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

Thank you.

-1

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

This is the Context class ,where you will 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 will 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 done in the database ,it is easier to let the Entity Framework itself generate these classes.

Browser other questions tagged

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