RWHepburn
RWHepburn

Reputation: 405

Breeze is not deserializing entity change sets when saving changes

I am using Breeze against a standard RPC style WebAPI. I have decorated the controller with the [BreezeController] attribute. I have defined entity metadata on the client for the entities being returned by the WebAPI actions. My Breeze DataService is setup as follows:

var dataService = new breeze.DataService({
    serviceName: "http://localhost/api/PartReceiptPurchaseOrders",
    hasServerMetadata: false
});

When calling the EntityManager's SaveChanges method after modifying an entity, the EntityInfo object on the server is empty. It appears the serialized entity passed to the SaveChanges method is not being deserialized properly into the expected entity on the server. I'm having a hard time understanding what I'm doing wrong.

When I inspect the JObject saveBundle argument passed to the SaveChanges method on the WebAPI controller, I get the expected entity details:

{  
    "entities": [{ 
        "PurchaseOrderPartId": 1, 
        "PartNumber": "ABC", 
        "SupplierPartNumber": "12345", 
        "Description": "Some Part", 
        "Bin": "1", 
        "Qty": 24, 
        "QtyReceived": 24, 
        "QtyBackordered": 0, 
        "Cost": 60, 
        "Currency": "USD",
        "PurchaseOrderId":1, 
        "entityAspect": { 
            "entityTypeName": "PurchaseOrderPart:#MyApp.Models", 
            "entityState": "Modified",
            "originalValuesMap": { 
                "QtyReceived": 0 
            }, 
            "autoGeneratedKey":{
                "propertyName": "PurchaseOrderPartId",
                "autoGeneratedKeyType": "Identity"
            }
        }    
    }],  
    "saveOptions": { "allowConcurrentSaves": false }
}

However, after the call to the base class method Breeze.WebApi.ContextProvider.SaveChanges() the entityInfo.Entity property contains an empty object as follows:

entityInfo {Breeze.WebApi.EntityInfo}   
    AutoGeneratedKey: null {Breeze.WebApi.AutoGeneratedKey}
    Entity {MyApp.Models.PurchaseOrderPart}
        Bin: null
        Cost: 0
        Currency: null
        Description: null
        PartNumber: null
        PurchaseOrder: null {MyApp.Models.PurchaseOrder}
        PurchaseOrderId: 0
        PurchaseOrderPartId: 0
        Qty: 0
        QtyBackordered: 0
        QtyReceived: 0
        SupplierPartNumber: null

If I breakpoint into the CreateEntityInfoFromJson in the Breeze.WebApi.ContextProvider class, I see that the call to jsonSerializer.Deserialize(new JTokenReader(jo), entityType) sets entityInfo.Entity to an empty entity object. There is no error raised during the deserialization so I can't tell why this is happening.

Can anyone point me towards a possible resolution?

Thanks, Richard

Upvotes: 0

Views: 358

Answers (1)

RWHepburn
RWHepburn

Reputation: 405

Ok, I figured this out and it was a dumb mistake on my part. My entity type on the server had been declared with internal setters like public decimal QtyReceived { get; **internal** set; }. This meant the JSON deserializer couldn't set the property value. Interestingly enough, the error is just ignored by the deserilizer.

Changing the setters to be public fixed this issue.

Upvotes: 3

Related Questions