1
votes

I have a simple object model public class License {

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string CreationUserId { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string LastModifiedUserId { get; set; }

    public string LicenseName { get; set; }

    public LicenseType LicenseType { get; set; }

    public State State { get; set; }

    public DateTime DateIssued { get; set; }

    public int ValidFor { get; set; }


}

public class State
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string CreationUserId { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string LastModifiedUserId { get; set; }

    [StringLength(2)]
    [Required]
    public string Name { get; set; }

    [Display(Name = "Long Name")]
    [Required, StringLength(25)]
    public string LongName { get; set; }
}

public class LicenseType
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string CreationUserId { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string LastModifiedUserId { get; set; }

    [StringLength(100), Required]
    public string Description { get; set; }
}

I am using the hot towel template breeze, durandal, knockout. I have a simple add view model

var _licenseAdded = false;
var vm = {

    states: ko.observableArray(context.states),
    licenseTypes: ko.observableArray(context.licenseTypes),

    viewAttached: function () {
        var self = this;
        $('input[name^="date"]').datepicker();
        $('#validFor').spinner({
            min: 365,
            max: 3650,
            step: 30
        });
        log('add Attached', null, true);

    },

    activate: function () {
        var self = this;
        self.original = context.manager.createEntity('License', { licenseName: 'Testing321', dateIssued: moment().format('L') }, null);
        log('add Activated', null, true);


    },
    canDeactivate: function () {
        if (_licenseAdded === false) {
            return app.showMessage('Are you sure you want to leave this page?', 'Navigate', ['Yes', 'No']);
        } else {
            return true;
        }
    },
    saveChanges: function () {

        $.when(context.saveNewLicense()).done(function () {
            _licenseAdded = true;

        });
        router.navigateTo('home');
    },
    original: undefined

};
return vm;

And here is my add.html, everything binds up fine and works beautifully until saving.

When I call saveChanges the saveBundle sent to the controller has no navigation properties attached that allow for the correct State and LicenseType to be stored I only get: saveBundle { "entities": [ { "Id": -1, "CreationUserId": null, "LastModifiedUserId": null, "LicenseName": "new for testing", "DateIssued": "2013-03-11T04:00:00Z", "ValidFor": 0, "entityAspect": { "entityTypeName": "License:#Volt.Telecom.Licensing.Models", "entityState": "Added", "originalValuesMap": {}, "autoGeneratedKey": { "propertyName": "Id", "autoGeneratedKeyType": "Identity" } } } ], "saveOptions": { "allowConcurrentSaves": false } }

Don't think I can get much more vanilla than this. Why might this occur? When I am debugging on the client the state and licenseType navigationProperties are all correct and with the correct values.

1

1 Answers

0
votes

I think the issue is that your EF model uses 'Independent Associations' instead of 'Foreign Key Assocations' (foreign key associations are the EF default). We do need to do a better job of documenting this assumption.

The reason for this requirement, which can be bypassed but with a substantial loss of functionality, is that the existence of foreign keys on the client is what allows breeze to automatically fix up relationships between entities that might be queried separately.

See the following MSDN article for more background: foreign-keys-in-the-entity-framework