Error: Entitytype<T> has no key defined. Define the key for this Entitytype

Asked

Viewed 1,819 times

1

When running my application, the following error is generated:

One or more validation errors Were Detected During model Generation: Ballpoint.DataAccess.Context.Booktable: Entitytype 'Apostaestadomap' has no key defined. Define the key for this Entitytype. Book1: Entitytype: Entityset 'Bookstate1' is based on type 'Bookstateomap' that has no Keys defined.

Class Apostatate

public class ApostaEstado
{
    public int Id { get; set; }
    public string Nome { get; set; }
}

Class Apostaestadomap

public class ApostaEstadoMap: EntityTypeConfiguration<ApostaEstado>
{
    public ApostaEstadoMap()
    {
        this.ToTable("ApostaEstado");
        this.HasKey(x => x.Id);

        //propriedades
        this.Property(x => x.Id).HasColumnName("ApostaEstadoId");
        this.Property(x => x.Nome).HasColumnName("ApostaEstadoNome");
    }
}

Script of the Apostaestado table

CREATE TABLE ApostaEstado
(
    ApostaEstadoId int not null auto_increment,
    ApostaEstadoNome varchar(30) not null,

    primary key(ApostaEstadoId)
)Engine = InnoDB;

Datacontext

public class DataContext : DbContext
{
    public DataContext() : base("name=ConnString") { }

    public virtual DbSet<TimeFutebol> TimesFutebol { get; set; }
    public virtual DbSet<Usuario> Usuario { get; set; }
    public virtual DbSet<Campeonato> Campeonato { get; set; }
    public virtual DbSet<PerfilAcesso> PerfilAcesso { get; set; }
    public virtual DbSet<Bolao> Bolao { get; set; }
    public virtual DbSet<SystemConfig> SystemConfig { get; set; }
    public virtual DbSet<Rodada> Rodada { get; set; }
    public virtual DbSet<Partida> Partida { get; set; }
    public virtual DbSet<RodadaEstado> RodadaEstado { get; set; }
    public virtual DbSet<ApostaEstadoMap> ApostaEstado { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Configurations.Add(new UsuarioMap());
        modelBuilder.Configurations.Add(new TimeFutebolMap());
        modelBuilder.Configurations.Add(new CampeonatoMap());
        modelBuilder.Configurations.Add(new BolaoMap());
        modelBuilder.Configurations.Add(new PerfilAcessoMap());
        modelBuilder.Configurations.Add(new SystemConfigMap());
        modelBuilder.Configurations.Add(new RodadaMap());
        modelBuilder.Configurations.Add(new PartidaMap());
        modelBuilder.Configurations.Add(new RodadaEstadoMap());
        modelBuilder.Configurations.Add(new ApostaEstadoMap());

        base.OnModelCreating(modelBuilder);
    }
}

For the Apostatate entity, I did EXACTLY the same process as the other entities. As for example the entity Rodadaestado, which does not return any error. It follows structure of the entity Rodadaestado:

Class Wheelsstade

public class RodadaEstado
{
    public int Id { get; set; }
    public string Nome { get; set; }

    public virtual ICollection<Rodada> Rodadas { get; set; }

    public RodadaEstado()
    {
        Rodadas = new HashSet<Rodada>();
    }
}

Class Rodadaestadomap

public class RodadaEstadoMap: EntityTypeConfiguration<RodadaEstado>
{
    public RodadaEstadoMap()
    {
        this.ToTable("RodadaEstado");
        this.HasKey(x => x.Id);

        //propriedades
        this.Property(x => x.Id).HasColumnName("RodadaEstadoId");
        this.Property(x => x.Nome).HasColumnName("RodadaEstadoNome");
    }
}

Table script Rodadaestado

CREATE TABLE RodadaEstado
(
    RodadaEstadoId int not null auto_increment,
    RodadaEstadoNome varchar(30) not null,

    primary key(RodadaEstadoId)
)Engine = InnoDB;

I really don’t know what to do. I have reviewed several times the classes Apostaestado, Apostaestadomap, Datacontext and the bank script, to see if I was doing/writing something wrong. However, I did not find any structure or code errors.

PS: I tried the Data Annotation alternative by associating the [Key] notation with the Id attribute. also failed.

  • There is an error in the Dbset statement if you placed the Aostaestadomap Map class. !!! give a look

  • @Virgilionovic I looked again and did not identify this error of writing.

  • 1

    Ultimo Dbset type this wrong public virtual Dbset<Apostaestadomap> ....

  • 1

    Holy Virgilio!!! It worked. Thank you very much!

  • 1

    Could you comment as a reply for me to assign as valid your reply?

1 answer

5


You have mistakenly placed the configuration class in place of the entity class.

It’s like this:

public virtual DbSet<ApostaEstadoMap> ApostaEstado { get; set; }

Switch to:

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

Browser other questions tagged

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