breezejs: navigation property not added to entity

I've configured my WebAPI ODATA service (using 5.0.0-rc1 for $expand and $select support) and everything seems to work fine but navigation properties.

The metadata does contain my navigation property (OpenPositions on Mandate) :

enter image description here

Then my breeze query is the following:

function search() {       
    var query =  breeze.EntityQuery.from("Mandates").expand("OpenPositions").inlineCount();

    return manager.executeQuery(query.using(service)).then(function (result) {
        logger.info(result);
    }).fail(function (error) {
        logger.error(error);
    });
}

The WebAPI controller :

[Queryable(AllowedQueryOptions= AllowedQueryOptions.All)]
    public override IQueryable<Mandate> Get()
    {
          return new List<Mandate>() { new Mandate() { 
            Id = 1, 
            PolicyNumber = "350000000",
            OpenPositions = new List<OpenPosition>(){ 
                new OpenPosition(){ Id = 1, Amount = 2300, Mandate_Id = 1 },
                new OpenPosition(){ Id = 2, Amount = 2100, Mandate_Id = 1 }
            }},
            new Mandate() { 
                Id = 2, 
                PolicyNumber = "240000000" ,
                OpenPositions = new List<OpenPosition>(){ 
                new OpenPosition(){ Id = 3, Amount = 2500, Mandate_Id = 2 },
                new OpenPosition(){ Id = 2, Amount = 2100,  Mandate_Id = 2}
            }

            } }.AsQueryable<Mandate>();
    }

Nothing spectacular. But although my Mandate entities are coming back in the resultset, they do not have their OpenPositions collection.

As a test, if I add .select("OpenPositions") to my breeze query, then I get an error:

unable to locate property: OpenPositions on entityType: Mandate:#WebAPINoBreeze.Models

Why could that be ?

[EDIT] query.entityType.NavigationProperties is an empty array, so that's probably a clue... It seems breeze could not build navigationproperties out of the metadata.

[EDIT]

foreign key added. problem still there:

 public class Mandate
{
    public int Id { get; set; }
    public string PolicyNumber { get; set; }
    public EStatus Status { get; set; }
    public virtual  List<OpenPosition> OpenPositions { get; set; }
}

public class OpenPosition
{
    public int Id { get; set; }
    public decimal Amount { get; set; }
    [ForeignKey("Mandate")]
    public int Mandate_Id { get; set; }
}

**[EDIT] **

For some reasons the [ForeignKey("Mandate")] attribute was removed at compile time (I think it's because the model class is generated. I've found a workaround and the metadata now contains the foreign key MandateId to Mandate in the OpenPositions :

enter image description here

Answers:

Answer

You must define a foreign key as Breeze associations require FKs. http://www.breezejs.com/documentation/navigation-properties

[EDIT]

Your bi-directional association should look like this:

 public class Mandate
{
    public int Id { get; set; }
    public string PolicyNumber { get; set; }

    public virtual  ICollection<OpenPosition> OpenPositions { get; set; }
}

public class OpenPosition {
    public int Id { get; set; }
    public decimal Amount { get; set; }

    public int MandateId { get; set; }
    public Mandate Mandate {get; set; }
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.