0

So using entity-framework, code first, I got two tables.

public class Course
{
    [Key, MaxLength(10)]
    public string CourseId { get; set; }

    [Required]
    public DateTime Created { get; set; }

    [Required]
    public bool Active { get; set; }


    public virtual IEnumerable<CourseText> CourseText { get; set; }
}

public class CourseText
{
    [Key, Column(Order=0)]
    public string CourseId { get; set; }

    [Key, Column(Order=1)]
    public string LanguageCode { get; set; }

    public string CourseName { get; set; }

    public string CourseDescription { get; set; }


    public virtual Course Course { get; set; }

    public virtual Language Language { get; set; }
}

And in my controller I am trying to query the information, but I only want one row for the CourseText because a course can have multiple languages.

    public IEnumerable<Course> GetCourses(string languageCode)
    {
        return DbSet
            .Where(a => a.Active)
            .Include(a => a.CourseText)
            .Include(a => a.CourseText.Where(b => b.CourseId == a.CourseId && b.LanguageCode == languageCode))
            ;
    }

But I keep getting the following error:
Message=The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

Please tell me what I am doing wrong.

Thomas
  • 345
  • 1
  • 6
  • 19
  • Why are you trying to filter a Course's Texts by the Course's Id? By definition they have the same `CourseId`. This isn't including either, you are trying to *filter* the related *entities*. – Panagiotis Kanavos Nov 24 '15 at 11:14
  • You can not do conditional includes like you are doing. I would recommend reading over this question: http://stackoverflow.com/questions/1535443/ef-query-with-conditional-include – Thewads Nov 24 '15 at 11:14

1 Answers1

1

The Include path expression must refer to a navigation property defined on the type.

Since you put Where expression in the Include args, you get this error. Because Where returns IEnumerable<T> which is not a navigation property.

You could make it like this :

public IEnumerable<Course> GetCourses(string languageCode)
    {
        return DbSet
            .Where(a => a.Active)
            .Include(a => a.CourseText)
            .Include(a => a.CourseText)
            .Where(b => b.CourseText.LanguageCode == languageCode);
    }

This assumes CourseText and Course are dblinked with CourseId

Perfect28
  • 11,089
  • 3
  • 25
  • 45