I am using Entity Framework only for metadata generation, I am experiencing an issue that the breeze query is not expanding the navigation property. In this many-to-many relationship example, the property StudentsCourses in Student is returned as null in the breeze query results. Could it be because I am not using EF for data access? Thanks!
public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
[InverseProperty("Student")]
public virtual ICollection<StudentCourse> StudentsCourses { get; set; }
}
public class Course
{
public int CourseID { get; set; }
public string Name { get; set; }
[InverseProperty("Course")]
public virtual ICollection<StudentCourse> StudentsCourses { get; set; }
}
public class StudentCourse
{
//[Key, Column(Order = 0)]
public int StudentID { get; set; }
//[Key, Column(Order = 1)]
public int CourseID { get; set; }
[ForeignKey("CourseID")]
[InverseProperty("StudentsCourses")]
public virtual Course Course { get; set; }
[ForeignKey("StudentID")]
[InverseProperty("StudentsCourses")]
public virtual Student Student { get; set; }
}
class StudentMap : EntityTypeConfiguration<Student>
{
public StudentMap()
{
HasRequired(p => p.StudentsCourses).WithMany().HasForeignKey(p => p.StudentID);
}
}
class StudentCourseMap : EntityTypeConfiguration<StudentCourse>
{
public StudentCourseMap()
{
HasKey(pc => new { pc.CourseID, pc.StudentID });
Property(pc => pc.CourseID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(pc => pc.StudentID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
The controller:
[BreezeController]
public class SchoolController : ApiController
{
private readonly SchoolRepository _repository;
public SchoolController() : this(null) { }
public SchoolController(SchoolRepository repository)
{
_repository = repository ?? new SchoolRepository();
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _repository.SaveChanges(saveBundle);
}
[HttpGet]
public IQueryable<Student> Students()
{
return _repository.Students;
}
[HttpGet]
public IQueryable<StudentCourse> StudentsCourses()
{
return _repository.StudentsCourses;
}
}
The implementation of SchoolRepository.Students:
public IQueryable<Student> Students
{
get
{
return SchoolContext.Students().AsQueryable();
}
}
The breeze query:
var query = breeze.EntityQuery.from("Students")
.where("Name", "==", "H")
.expand("StudentsCourses");
SchoolRepository.Students
? – Jeremy Danyow