breezejs: navigation property not added to entity

无人久伴 提交于 2019-12-02 12:11:44

问题


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) :

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 :


回答1:


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; }
}


来源:https://stackoverflow.com/questions/18512278/breezejs-navigation-property-not-added-to-entity

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