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'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With