jvrdelafuente
jvrdelafuente

Reputation: 2002

Error Loading related entities on demand (entityAspect.loadNavigationProperty())

I am doing a application using durandaljs and breezejs. I am trying to get the value of a Navigation Properties. My problem is that when I tryied to call this method in breeze:

user.entityAspect.loadNavigationProperty("userType");

I get the error ""The 'propertyOrExpr' parameter must be a 'string'"". This error is throw for the follow section of code:

var q = new EntityQuery(navProperty.entityType.defaultResourceName);
var pred = buildNavigationPredicate(entity, navProperty);
q = q.where(pred);

The problem is that 'pred' is empty, because 'buildNavigationPredicate' return null. Getting into the 'buildNavigationPredicate':

if (navigationProperty.foreignKeyNames.length === 0) return null;

The problem is that the 'foreignKeyNames' array is empty, all the rest field in the 'navigationProperty' are filled, but I don't know why, but my server doesn't send this information. (Also the foreignKeyNamesServer is empty).

Do somebody have the same problem? can be it a bug of breeze?

Thanks.

Upvotes: 1

Views: 728

Answers (2)

jvrdelafuente
jvrdelafuente

Reputation: 2002

I found the solution yesterday and I din't have time to refresh this. The problem was in the definition of my user class. I am using Web API and EF Code first. My old user class was:

    public class User
{
    public Guid Id { get; set;}
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool Active { get; set; }

    public virtual UserType UserType { get; set;}
}

To do work the user.loadNavigationProperty('userType'), I had to change the old user class in this:

    public class User
{
    public Guid Id { get; set;}
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool Active { get; set; }

    public Guid UserType_Id { get; set; }

    [ForeignKey("UserType_Id")]
    public virtual UserType UserType { get; set;}
}

And doing that, I solved the problem. However, I can implement this solution for many to many relations. Doing a little of research, I have discovered, that Many to Many relations are not supported currently for breeze. So the only thing that I can do is implement a middle entity in many to many relation.

Very thanks Ward for your time. If there are any other way to solve that problem, I would be very pleased to listen. Thanks.

Upvotes: 2

Ward
Ward

Reputation: 17863

The error message is not helpful. Fortunately, you've traced the problem to the lack of foreignKeyNames. That suggests that you are missing something in the definition of the navigation property. For some reason, Breeze isn't detecting that the FK property in User that relates it to a UserType. If you are using Web API and EF, you might confirm that the FK is properly specified on the server.

Actually, to help properly, we'd have to see your class definition of User, the portion of the metadata from the server that describes User and UserType (PLEASE DO NOT print the entire metadata!) and a snapshot of the EntityType object returned at runtime from manager.metadataStore.getEntityType('User')

Upvotes: 0

Related Questions