Luis Tellez
Luis Tellez

Reputation: 2983

Populating Navigation Properties Collection Lazy

I have two related classes in a one-to-many relationship. I can access the Class property in one of the classes, but not the other.

Here are my classes:

 public  class Seccion
    {

        public int Id { get; set; }           
        public string  Nombre { get; set; }   
       public  ICollection<TipoCaracteristica> TiposCaracteristicas;

    }


    public class TipoCaracteristica
    {
        public int Id { get; set; }         
        public int SeccionId { get; set; }
        [ForeignKey("SeccionId")]
        public Seccion Seccion { get; set; }
        public List<CaracteristicaAdicional> Caracteristicas;
    }

With this code i can include the Seccion properties

 public IQueryable<TipoCaracteristica> GetTipoCaracteristicas
        {
            get { return db.TipoCaracteristicas.Include("Seccion"); }

        }

I haven't been able to get a list of Seccion that includs the list of TipoCaracteristicas. I also need to get the list of Caracteristicas for each TipoCaracteristicas.

This is what I'm trying

public IQueryable<Seccion> GetSecciones
        {
            get { return db.Secciones.Include("TiposCaracteristicas"); }
        }

But I get this error:

A specified Include path is not valid. The EntityType 'Seccion' does not declare a navigation property with the name 'TiposCaracteristicas'.

How can I do this? Can I include the other level too?

Thanks in advance for your time.

Upvotes: 0

Views: 512

Answers (1)

Travis J
Travis J

Reputation: 82287

What you need to do is inform Entity Framework that your properties are Navigation Properties by using the virtual keyword. You also need to provide the access modifiers:

public virtual ICollection<TipoCaracteristica> TiposCaracteristicas { get; set; }

Upvotes: 2

Related Questions