简体   繁体   中英

Breeze - self referencing entity

i get some problem using Breeze to execute queries via my ASP.NET web api application.

Here is my entity definition that i want to request on :

[Serializable]
[DataContract]
public class Subject
{

    public Subject()
    {
        Subjects = new List<Subject>();
    }
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public Subject Parent { get; set; }

    [DataMember]
    public IList<Subject> Subjects { get; set; }
}

and here is the query in my datacontext.js file

var query = EntityQuery.from("Subjects");

            manager.executeQuery(query)
            .then(function (data) {                    
                // do something with data.results
            })
            .fail(function (error) {

            });

but the query always fails with an error saying "expected object"

All other queries on other "simple" entities works fine. If i remove the properties "Parent" and "Subjects" from my Subject Entity, the query works.

Does anyone have an idea ?

Thanks !

Breeze needs a foreign key in order to fix up the relations between entities and you're missing it in your Subject class definition:

[DataMember]
public System.Nullable<int> ParentId { get; set; }

Or, if you are using non-conventional naming, be sure to add the ForeignKey tag to the navigation:

[DataMember]
[ForeignKey("FKParentId")]
public Subject Parent { get; set; }

You could also define it via Fluent Interface. You will find more on that at http://msdn.microsoft.com/en-us/data/hh134698.aspx .

Thanks !

i added : [DataMember] public System.Nullable<int> ParentId { get; set; } [DataMember] public System.Nullable<int> ParentId { get; set; }

and it works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM