Relationship 1: N using EF using Data Anotation

1

I am trying to map between these two classes, where in championship I have a list of all steps related to the championship

Follow the classes,

[Table("ETAPA")]
public class Etapa
{
    [Key]
    public int Id { get; set; }
    [Column("ETAPA_NUMERO")]
    public int EtapaNumero { get; set; }
    [Column("DATA_EVENTO")]
    public DateTime DataEvento { get; set; }
    [Column("CIDADE")]
    public string Cidade { get; set; }
    [Column("STATUS")]
    public int Status { get; set; }
    [Column("FKID_CAMP")] //minha FK**
    public int IdCamp { get; set; }

    [ForeignKey("IdCamp")]
    public virtual Campeonato Campeonato { get; set; }
}

and a

[Table("CAMPEONATO")]
public class Campeonato
{
    public Campeonato()
    {
        Etapas = new HashSet<Etapa>();                
    }
    [Key]
    [Column("ID")]
    public int Id { get; set; }
    [Column("DESCRICAO")]
    public string Descricao { get; set; }
    [Column("ANO")]
    public int Ano { get; set; }        

    public virtual ICollection<Etapa> Etapas { get; set; }

}

I need to know how to make this relationship using EF 6.0

    
asked by anonymous 21.04.2017 / 20:52

1 answer

2

As per question clarified by comment , there is no problem with the mapping itself, which is correct. When bringing in the stages of a league, it is recommended to rush the load using .Include() :

var campeonato = db.Campeonatos
                   .Include(c => c.Etapas)
                   .FirstOrDefault();

Also remove the code below, which is affecting the lazy load:

public Campeonato()
{
    Etapas = new HashSet<Etapa>();                
}
    
21.04.2017 / 21:17