Breeze.js: adding an element to a navigation property collection without inverse rel raises an exception [Unable to get value of the property 'name']

萝らか妹 提交于 2020-01-04 02:36:45

问题


I have two entities in my EDM, Application and Address pretty much like the following:

class Application
{
    ICollection<Address> Addresses { get; set; }
}

class Address { }

On the client, I create an instance of each and try to add the address instance to the Application.addresses collection:

var address = addressType.createEntity(...);
var application = applicationType.createEntity(...);

application.addresses.push(address);

Unfortunately, I get a runtime exception saying: "Unable to get value of the property 'name': object is null or undefined".

I tracked the exception back to the checkForDups function in breeze.debug.js@9393-9404 (v1.2.8):

function checkForDups(relationArray, adds) {
    // don't allow dups in this array. - also prevents recursion 
    var inverseProp = relationArray.navigationProperty.inverse;
    var goodAdds = adds.filter(function(a) {
        if (relationArray._addsInProcess.indexOf(a) >= 0) {
            return false;
        }
        var inverseValue = a.getProperty(inverseProp.name);
        return inverseValue != relationArray.parentEntity;
    });
    return goodAdds;
}

As it happens, my entities are in a one-to-many unidirectional relationship (without an inverse navigation property); as a result at runtime relationArray.navigationProperty.inverse is undefined and so the error when trying to access the name property.

Adding a simple check fixes the problem, and allows adding to the collection:

if (!inverseProp) {
    return true;
}

So after all this the question is: is this a bug or is it simply that Breeze does not support one-to-many unidirectional?


回答1:


Edit As of v Breeze 1.3.5, available now (June 4 2013), this has been fixed.


Edit: Ok, this IS a bug, but I couldn't get the fix in for this current release. I will try to get it in the following release.

The fix that you suggested, which was a good idea, actually only hides the issue.

The real problem is that breeze does not have sufficient metadata for the case where we have a unidirectional navigation in the 1->n direction (i.e. not in the n->1 direction). Because of this, duplicate entity checking in the navigation collection will not work and automatic hookup of children to parents will be missing as well.

The simplest work around until we get the fix in is to simply make it a bidirectional navigation. Note that unidirectional navigation in the other direction works just fine.


This is likely a bug

We do have an example in our DocCode sample project of a unidirectional navigation between OrderDetails and Products. But in that case we allow navigation from a OrderDetail -> Product (1-1) but not from Product -> OrderDetails (1-n).

Your case appears to be the opposite, i.e. allowing 1-n but disallowing the corresponding 1-1. I will create some tests and if I can repro this, it will be fixed in the next release.

I will post back here when that occurs. ( and thx for finding it :)



来源:https://stackoverflow.com/questions/15909589/breeze-js-adding-an-element-to-a-navigation-property-collection-without-inverse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!