0

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");
11
  • What does your api controller look like? Dec 28, 2014 at 14:17
  • I have just included the api controller as shown above, thanks! Dec 28, 2014 at 22:26
  • what's the implementation of SchoolRepository.Students? Dec 29, 2014 at 18:52
  • The implementation of SchoolRepository.Students has been included, thanks! Dec 29, 2014 at 21:17
  • I don't see the problem- have a look at the troubleshooting tips in the "when .expand(...) fails" section of this page: getbreezenow.com/documentation/query-result-debugging#expand Dec 30, 2014 at 1:37

1 Answer 1

0

I think your FIRST challenge lies in conveying the query information from the client to the server such that your particular data access technology can do the right thing.

You're not using EF. You were not clear about whether you can use LINQ to access the data that you want. I gather by looking at your Students controller method that you are NOT applying the LINQ expression on your data tier:

public IQueryable<Student> Students
{
    get
    {
        return SchoolContext.Students().AsQueryable();
    }
}

You are pulling EVERY student in your database into server memory and then letting Web API apply the LINQ query to whittle it down before shipping students over the wire.

That's OK if you don't have a lot of students. That's awful if you do.

It will be even more awful if you try to pull all of their Courses into server memory at the same time.

It appears from your comments that you have additional difficulties trying on the server to wire up the navigation properties between students and their course maps and between those maps and the courses. Apparently your data access layer doesn't do this for you or there is some other impediment that you haven't mentioned.

I might have a more apt solution for you if I knew more about your actual data access options.

It would also help to know how you actually use this API. If you don't need the full power of LINQ (or can't really use it), maybe you should simply create the endpoint that you need.

Web API Controller

Let's suppose - I'm guessing now - that you only ever query for "Students and their courses" with one or two filter criteria and never with ordering or paging.

Your example - "Get all students whose name starts with 'H' and get their courses" - that sounds kind of artificial to me. But I'll bite. Let's also support filtering by StudentID

You could write a Web API controller method that takes these two criteria as parameters. Your repository doesn't have to wire everything together. It just has to know how to apply the selection criteria. Here's one possibility.

[HttpGet]
public object StudentsAndCourses(int? studentId, string? studentName)
{
    var students = _repository.GetStudents(studentId, studentName);

    // once we know the students, we can get the course maps
    var courseMaps = _repository.GetCourseMaps(students);

    // now get the courses for those maps
    var courses = _repository.GetCourses(courseMaps);

    return new { students, courseMaps, courses};
}

As you can see, I'm just throwing the entities you need into a bag and sending the bag to the client. This method does not return an IQueryable. Therefore, the OData query string (if there were one) is effectively ignored. The Web API does not attempt to compose or apply a LINQ expression from an OData query string.

In an effort to keep this example relatively simple, I'm not talking about Breeze's ability to represent a query as a JSON object.

That would be ideal if you really needed rich query semantics on the server but couldn't use LINQ. It's much easier to parse the Breeze JSON representation of a query than it is to to tear apart the OData URL query string or spelunk .NET LINQ expression trees.

It's overkill if you don't need that richness ... and I'll assume that you don't.

Breeze Client

Let's query by student name

return EntityQuery.from('StudentsAndCourses')
    .withParameters({studentName: 'H'})
    .using(manager).execute()
    .then(success).catch(handleFail);

And now query by student ID

return EntityQuery.from('StudentsAndCourses')
    .withParameters({studentId: 42})
    .using(manager).execute()
    .then(success).catch(handleFail);

The success callback is the same for both. It's job is to return the selected Student(s) to the caller:

function success(data) {
    return data.results.students
}

The CourseMap and Course entities are also in the data.results bag. You don't care. You are content that Breeze discovered these while processing the bag and wired up the related entities.

Suppose that you had a method in your client-side dataservice called getStudentInfoById. You call it from within your ViewModel like so:

vm.student;
...
dataservice.getStudentInfoById().then(function(student) {
    vm.student = student;
});

When the query succeeds, the following (Angular model) properties will be populated

vm.student;
vm.student.StudentCourses;
vm.student.StudentCourses[0].Course;

You can also see all courses that you've retrieved with this or a similar query:

manager.getEntities(['Course']);

If you don't have too many courses, you might just get them all upfront when your app starts. Then you can simplify the StudentsAndCourses controller method such that it returns only Students and their StudentCourse maps.

I leave this to you as an exercise.

Do you really need EF Metadata generation?

I'll bet that your model is much more complicated than the one shown in your question.

If your model is actually reasonably small, you might consider writing the metadata by hand on the client. You might find it to be both easy and more simple.

9
  • Much appreciated for your detailed example. I underestimated Breeze, I have done a test, it did wire up the the navigation properties, that is awesome!! I saw the Breeze example for writing the metadata by hand, I used EF instead to generate metadata I think that is easier. I have another question, the navigation property looping, Student -> StudentCourses -> Student -> StudentCourses, is it a problem or it is fine? Thanks heaps! Jan 1, 2015 at 11:23
  • Forgot to mention, I am still designing the database, there are only two student records so I just return all. There will be filtering parameters for sure. Jan 1, 2015 at 11:34
  • Circular references are not a problem for Breeze ... far from it ... they are expected! Is there a reason you aren't using EF and SQL Server? It's pretty much the happy path for folks programming in .NET and you are using the Web API apparently
    – Ward
    Jan 1, 2015 at 18:12
  • I am using SQL Server and Web API, but not EF because it is heavy, it forces foreign key relationship, for performance purpose I store some foreign key data as normal data, and I have to keep data integrity to avoid dummy data. Can I please ask one last question, as circular references are not a problem, is there a potential issue that cascade delete would delete more data than it should? Thanks! Jan 1, 2015 at 23:09
  • I confess I have no idea what people mean when they call S/W "heavy". I don't understand "awesome" either. I suppose by "FK data as normal data" you mean that your denormalize some of your data. That's fine; you just have to manage it. That's not an EF issue. So you lost me here. Perhaps you are misinformed? Well ... whatever ... that's certainly a topic for some other forum.
    – Ward
    Jan 2, 2015 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.