I'm starting in the Entity Framework and when trying to load an entity state, the relational parent entity is coming with null value, what could I adjust in my code to solve?
Class Status:
public class Estado
{
...
public int Id { get; set; }
public string Nome { get; set; }
public Pais Pais { get; set; }
}
Country Class:
public class Pais
{
...
public int Id { get; set; }
public string Nome { get; set; }
}
Mapping Status:
public class EstadoConfiguracao: EntityTypeConfiguration<Estado>
{
public EstadoConfiguracao()
{
HasKey(e => e.Id);
Property(e => e.Nome).HasColumnName("Nome").HasMaxLength(50).IsRequired();
ToTable("estado");
HasRequired(e => e.Pais).WithRequiredPrincipal();
}
}
Country Mapping:
public class PaisConfiguracao: EntityTypeConfiguration<Pais>
{
public PaisConfiguracao()
{
HasKey(p => p.Id);
Property(p => p.Nome).HasColumnName("Nome").HasMaxLength(50).IsRequired();
ToTable("pais");
}
}
I am creating a method to receive the Id of a State as parameter and return the State with the Country:
public Estado ObterComPais(Int32 id)
{
return _context.Set<Estado>().Include(e => e.Pais).SingleOrDefault(e => e.Id == id);
}