Breezejs entity, knockout binding, null navigation properties

374 Views Asked by At

I know I am doing something stupid here but I'm hoping someone with some Breezejs experience can help me out (please) as I am new to it. So the problem is when I do a breeze manager.saveChanges() the navigation properties (and only the navigation properties) are not sent to the server. In my model all the navigation properties are just lookup items if that makes any difference and I am using knockout to do the binding. Here is a screenshot to show whats wrong:-

enter image description here

the relevant bit in my dataservice just does this:-

 function createAssessment() {               
   return manager.createEntity('VulnerabilityAssessment');       
}

Domain model:-

public class VulnerabilityAssessment
{      
    public int Id { get; set; }
    public virtual AssessmentStatus Status { get; set; }
    public virtual PupilRecord Pupil { get; set; }        
    public DateTime StartDate { get; set; }      
    public DateTime EndDate { get; set; }        
    public string LatestContact { get; set; }      
    public string Referrer { get; set; }      
    public virtual ReferralReason ReasonForReferral { get; set; }
    public string SingleResponsibleAdult { get; set; }
    public string CurrentIssues { get; set; }
    public virtual ReferralSupport PrimaryReferralSupport { get; set; }
    public virtual ReferralSupport SecondaryReferralSupport { get; set; }
    public bool LikelyToLeaveWithoutQualification { get; set; }
    public virtual Destination MovingOnDestination { get; set; }
    public virtual Destination DestinationAfterOneMonth { get; set; }
    public virtual Destination DestinationAfterSixMonths { get; set; }
    public virtual Destination DestinationAfterTwelveMonths { get; set; }
    public virtual ProtectedCharacteristic ProtectedCharacteristic { get; set; }
    public string Undisclosed { get; set; }


}

Here is my viewmodel:-

define(['services/logger', 'services/dataservice'], function (logger, dataservice) {

var title = 'Vulnerability Assessment';

this.activate = function activate() {
    var initialData = dataservice.getAssessmentLookups()
        .then(function (data) {
            vm.referralReasonCodes(data.referralReasonCodes);
            vm.referralSupportCodes(data.referralSupportCodes);
            vm.assessmentStatusCodes(data.assessmentStatusCodes);
            vm.destinations(data.destinations);
            vm.protectedCharacteristics(data.protectedCharacteristics);

            vm.assessment(dataservice.createAssessment());
            console.log('assessment: %A', vm.assessment());
        })
        .done();

    logger.log(title, null, title, true);

    return initialData;
}

this.save = function () {
    console.log('in save: %A', vm.assessment());
    dataservice.saveChanges();
}

var vm = {

    activate: this.activate,
    title: title,

    referralReasonCodes: ko.observableArray(),
    referralSupportCodes: ko.observableArray(),
    assessmentStatusCodes: ko.observableArray(),
    destinations: ko.observableArray(),
    protectedCharacteristics: ko.observableArray(),
    assessment: ko.observable(),
    save: this.save
};

return vm;

});

View/HTML:-

<select id="reason-for-referral" class="form-control input-md" data-bind="options: $root.referralReasonCodes, optionsText: 'description', value: $data.reasonForReferral"></select>

Note: the select element above is within a another div that goes like this:-

<div class="row" data-bind="with: assessment">

Here is a look at my object just before it is sent to the server. enter image description here

Totally stuck at the moment... Many thanks for taking a look.

1

There are 1 best solutions below

1
Jay Traband On BEST ANSWER

Breeze does not send "related" entities ( those available thru navigation properties) in a save UNLESS these related entities are "modified", "added" or "deleted". This is the default behavior when you call EntityManager.saveChanges without any parameters. The reason for this is that we don't want send any data that has not actually changed.

If, on the other hand, you are calling EntityManager.saveChanges and passing in a list of entities then you will need to include any related entities that you want send to the server as part of the list.

If not, then why do you want to send entities that have not changed to the server on a "saveChanges" call? Am I missing something?