Entity Framework 7 Acceso a objetos de entidad dentro de ICollection

asp.net-core-mvc c# entity-framework-core

Pregunta

Tengo dos entidades Usuarios y Direcciones. Direcciones es una propiedad de ICollection en las entidades Usuarios. Sin embargo, no puedo acceder a las direcciones individuales dentro de ICollection. ¿Es eso posible y solo tengo la sintaxis incorrecta o el framework de entidades no lo permite?

El código:

Usuarios:

 public partial class User
{
    public User()
    {
        Addresses = new HashSet<Address>();
        Comments = new HashSet<Comment>();
        Orders = new HashSet<Order>();
        Posts = new HashSet<Post>();
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

Direcciones:

 public partial class User
{
    public User()
    {
        Addresses = new HashSet<Address>();
        Comments = new HashSet<Comment>();
        Orders = new HashSet<Order>();
        Posts = new HashSet<Post>();
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

Repositorio (padre):

 public partial class User
{
    public User()
    {
        Addresses = new HashSet<Address>();
        Comments = new HashSet<Comment>();
        Orders = new HashSet<Order>();
        Posts = new HashSet<Post>();
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

UserRepository:

 public partial class User
{
    public User()
    {
        Addresses = new HashSet<Address>();
        Comments = new HashSet<Comment>();
        Orders = new HashSet<Order>();
        Posts = new HashSet<Post>();
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

El siguiente código no me dará acceso a la primera dirección en la propiedad ICollection en la entidad del usuario

 public partial class User
{
    public User()
    {
        Addresses = new HashSet<Address>();
        Comments = new HashSet<Comment>();
        Orders = new HashSet<Order>();
        Posts = new HashSet<Post>();
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

Por favor avise sobre cómo corregir mi código para que tenga acceso a las entidades en la colección.

Respuesta aceptada

En caso de que alguien se esté preguntando cómo resolví esto, aquí está la respuesta:

Como se mencionó por COLD TOLD, ICollection no admite arreglos como la indexación. Sin embargo, las listas lo cambian a una lista en lugar de a ICollection y eliminan el hashset que se creó en el custructor

 public partial class User
{
    public User()
    {
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual List<Address> Addresses { get; set; }
    public virtual List<Comment> Comments { get; set; }
    public virtual List<Order> Orders { get; set; }
    public virtual List<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

Esto me permite acceder a esas entidades anidadas como esta:

 public partial class User
{
    public User()
    {
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual List<Address> Addresses { get; set; }
    public virtual List<Comment> Comments { get; set; }
    public virtual List<Order> Orders { get; set; }
    public virtual List<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

Respuesta popular

No creo que sea una buena idea tener tantas incorporaciones en su selección, ya que puede causar problemas en su rendimiento, puede intentar cambiar el orden de la consulta

 return DbSet.Include(o => o.Orders)
            .Include(o => o.Addresses)
            .Where(o => o.Id == Id)
            .FirstOrDefault();



Licencia bajo: CC-BY-SA with attribution
No afiliado con Stack Overflow
¿Es esto KB legal? Sí, aprende por qué
Licencia bajo: CC-BY-SA with attribution
No afiliado con Stack Overflow
¿Es esto KB legal? Sí, aprende por qué