CRUD error with . NET MVC

Asked

Viewed 78 times

1

I am trying to recover some data from my database but is giving the following error

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

Erro que aparece quando tento recuperar

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; }

Style Class

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 configuration 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);
    }
  • You can edit your question and post the Model? It’s an index problem.

  • Ai @Ciganomorrisonmendez ??

1 answer

1


Do not use [Index] for primary keys. Does not work.

Change 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 must force your context setting 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 configuration is not ok. It would be nice if you review your web.config basing in the various questions that have already been asked about it here.

  • I’ll change, and see what happens..

  • Kept making the same mistake..

  • @Frankdantas Update your question with Usuario also, please. Do not use images in question. Paste the code and use the formatting guide.

  • @Frankdantas I updated the answer.

  • Even switching to [Key] still generated the index in the database, removed the index with sql even, but still the error continues..

  • @Frankdantas I updated the answer once again.

  • Opa.. Now yes, it was right, I mean, it is no longer giving the error, but it is not returning anything yet, even though it has data in the database.. But then it’s already subject for another topic, I’ll take another look here. o/

Show 2 more comments

Browser other questions tagged

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