CRUD error with .NET MVC

1

I'm trying to recover some data from my bank, but it's giving the following error

  

Specified key was too long; max key length is 767 bytes

I have no idea what's going on.

User Class

public class Usuario
{
    [Key]
    public int UsuarioId { get; set; }
    public string Email { get; set; }
    public string Senha { get; set; }
    public DateTime DataCadastro { get; set; }
    public string Nome { get; set; }
    public string Sobrenome { get; set; }
    public float Latitude { get; set; }
    public float Longitude { get; set; }
    public string Token { get; set; }
    public bool Confirmado { get; set; }
    public string Apelido { get; set; }
    public string Imagem { get; set; }
    public bool Privado { get; set; }
    public string ImagemCapa { get; set; }
    public DateTime DataNascimento { get; set; }
    public int Sexo { get; set; }
    public DateTime DataExpiracaoToken { get; set; }
    public int EstiloId { get; set; }
    public Estilo Estilo { get; set; }

Class Style

public class Estilo
{
    [Key]
    public int EstiloId { get; set; }
    public string Nome { get; set; }
    public string Local { get; set; }


    public virtual ICollection<Usuario> Usuarios { get; set; }

Style setting class for code-first

public EstiloConfig()
    {
        HasKey(a => a.EstiloId);

        HasMany(a => a.Usuarios).WithRequired(a => a.Estilo).HasForeignKey(a => a.EstiloId);

        Property(a => a.Nome).IsRequired().HasMaxLength(45);
        Property(a => a.Local).IsRequired().HasMaxLength(100);
    }
    
asked by anonymous 10.09.2015 / 21:13

1 answer

1

Do not use [Index] for primary keys. It does not work.

Modify to the following:

public class Estilo 
{
    [Key]
    public int EstiloId { get; set; }
    public string Nome { get; set; }
    public string Local { get; set; }

    public virtual ICollection<Usuario> Usuarios { get; set; }
}

EDIT

This answer here states that you should force your context configuration as follows:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class MeuContext : DbContext
{
    public MyContext()
        : base()
    {
        this.Configuration.ValidateOnSaveEnabled = false;
    }

    static MyContext()
    {
            DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }
}

Quite possibly the project setup is not ok. It would be nice to review your web.config based on on the various questions you have already were made about it here .

    
10.09.2015 / 22:08