Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from normalized tables

I would like to retrive data (list of channels for category).

Here's my model:

public class Category : AuditableEntity<int>
{
    public string Name { get; set; }

    public virtual IEnumerable<CategoryChannel> CategoryChannels { get; set; }
}

public class CategoryChannel : Entity<int>
{
    [Display(Name = "Channel")]
    public string ChannelId { get; set; }

    [ForeignKey("ChannelId")]
    public virtual Channel Channel { get; set; }

    [Display(Name = "Category")]
    public int CategoryId { get; set; }

    [ForeignKey("CategoryId")]
    public virtual Category Category { get; set; }
}

public class Channel : AuditableEntity<string>
{
    public string Name { get; set; }

    public virtual IEnumerable<CategoryChannel> CategoryChannels { get; set; } 
}

And here's my code for retrive data:

    return _entities.Set<Channel>()
        .Include(x => x.CategoryChannels).Where(y => y.Name.Equals(category))
        .AsEnumerable();

And finally I've got an error message:

A specified Include path is not valid. The EntityType 'xxx.Channel' does not declare a navigation property with the name 'CategoryChannels'.

like image 759
mskuratowski Avatar asked Sep 29 '26 20:09

mskuratowski


1 Answers

IEnumerable<T> can't be a navigation property used for lazy/eager loading, you'll have to use an implementation of ICollection<T> for that (Array, List, HashSet...) or just ICollection<T> itself of course.

IEnumerable is lazy by default because of deferred execution which is why you need an implementation which ensures a local copy of the collection.

like image 161
Alexander Derck Avatar answered Oct 01 '26 19:10

Alexander Derck