Search code examples
.netasp.net-web-api2.net-4.5breeze

BreezeController does not get the JObject (it is null)


I have a javascript app that is using BreezeJs.

When I call saveChanges, the entities get converted to javascript and sent over the wire. (I confirmed this with fiddler.)

But when I put a breakpoint in my Web Api Controller, the JObject for SaveChanges is null.

I can't seem to figure out why.

Here is an example of how my controller is setup:

[BreezeController]
public class ShipmentController : ApiController
{
    [HttpGet]
    public string Metadata()
    {
        return dataAccess.Metadata();
    }
    [HttpGet]
    public IQueryable<Shipment> Shipments()
    {
        return dataAccess.Shipments();
    }


    //   This is what is NULL each time --------+
                                                |
                                                |
    [HttpPost]                                  V
    public SaveResult SaveChanges(JObject saveJObject)
    {
        return dataAccess.SaveChanges(saveJObject);
    }

    //... Other stuff to get data
}

My queries to get data via the controller work fine. So it is not a total failure scenario.

And as I said above, Fiddler shows valid JSON going over the wire. But somehow the controller is not taking it.

My only guess is that I have several objects that I add to my client side breeze entities. But I have done that as the documentation says you should and they show up in the __unmapped section of the object's JSONin fiddler.

Does anyone have any ideas I can try to figure out why my Web Api controller is not getting the JObject?


Solution

  • Note that if you don't have navigation properties defined in your server-side model, you can create the navigation properties in the client-side metadata as long as you have the foreign key properties to work with:

        var childEntityType = metadataStore.getEntityType("MyChildEntity");
        childEntityType.addProperty(new breeze.NavigationProperty({
            name: "myParent",
            entityTypeName: "MyParentEntity:#My.Namespace",
            isScalar: true,
            associationName: "MyChildEntity_MyParentEntity",
            foreignKeyNames: ["myParentId"]
        }));
    

    You should add these relationships immediately after retrieving the metadata from the server, and before creating any entities or performing any queries.

        entityManager.fetchMetadata().then(function () {
            patchMetadata(entityManager.metadataStore); // add missing navigations
        });
    

    With the navigation property added, Breeze will automatically hook up your related entities upon query, so you don't need to create unmapped properties.