1
votes

Two days spent on this so far. It's driving me insane.

Some linked code-first entity framework classes:

public class Schedule
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid RowId { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public virtual ICollection<Charge> Charges { get; set; }
}

public class Charge
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid RowId { get; set; }
    public decimal ChargeableRate { get; set; }
    public DevType DevType { get; set; }
    public Schedule Schedule { get; set; }
}

public class DevType
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid RowId { get; set; }
    public string Type { get; set; }
    public virtual ICollection<Charge> Charge { get; set; }
}

I ask entity framework for a schedule like this:

        _schedule Schedule = _myContext.Schedules
                .Include("Charges.DevType")
                .Where(cs => cs.Start < dateWindow && cs.End > dateWindow)
                .First();

And I get back everything I need. When serialised to Json for a web service it all looks fine and dandy, with all the data as requsted.

As soon as it's passed to Breeze, however, I lose the DevType objects at the bottom of the stack. They're not null, or empty, they're simply NOT THERE.

Pausing in the browser and drilling into metadataStore._entityResourceMap you can find the DevType registered, so Breeze clearly knows it exists as part of the model. It just can't seem to see the relationship between Charge and DevType, so ignores it.

I've tried all sorts of stuff - registering the relationships explicity in EF (oddly results in added empty columns in the database), using the .expand command in Breeze to force it to load:

 function getCurrentChargingSchedule() {
var query = breeze.EntityQuery.from('GetCurrentSchedule').expand('Charge.DevType');
return manager.executeQuery(query);
}; 

None of it makes any difference.

The closest I've got was removing the virtual ICollection of Charge from DevType and instead marking it with:

[Key, ForeignKey("Charge"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]

This doesn't seem right - there is a many to one relationship here, so I shouldn't be removing the ICollection. And unsurprisingly queries to EF with this in place always returned Charge objects with DevType: null. But under this scenario Breeze did actually pick up the DevTypes - it's just that they were, obviously, null.

I'm on the verge of tearing up Breeze here and relying directly on my web service for data transfer. Can anyone please, please help?

1

1 Answers

0
votes

Have you tried loading the charges with Expand?

function getCurrentChargingSchedule() {
    var query = breeze.EntityQuery.from('GetCurrentSchedule').expand('Charges, Charges.DevType');
    return manager.executeQuery(query).fail(queryFailed);
};

function queryFailed (error) {
    console.log(error.message);
}

Your navigation property is Schedule > Charges so you should load them with the full name (Charges)