I'm experimenting with BreezeJS with Web API using the BreezeControllerAttribute. How should calculated properties on an entity be exposed? The only way I've found to do this reliably is to create an intermediate DTO that inherits from the entity or use a projection. Normally I would use a readonly property for this scenario, but those appear to be ignored.
相关问题
- Register MicroServices in Azure Active Directory (
- PromptDialog Choice with List object Bot Framework
- Dotnet Core API - Get the URL of a controller meth
- Serving data with “transfer-encoding: chunked” on
- StringEnumConverter invalid int values
相关文章
- POSTing data to WebApi after update to 5.1.0 fails
- Can you use generic methods in a controller?
- Remove a route with IOperationFilter in SwashBuckl
- Validate fails in unit tests
- subdomain CORS in webApi 2
- Post Array Json to .net core web api controller [d
- How can I read JSON from a StringContent object in
- Add claims with Owin Middleware
When Breeze maps JSON property data to entities, it ignores properties that it does not recognize. That's why your server class's calculated property data are discarded even though you see them in the JSON on the wire.
Fortunately, you can teach Breeze to recognize the property by registering it as an unmapped property. I'll show you how. Let me give some background first.
Background
Your calculated property would be "known" to the Breeze client had it been a property calculated by the database. Database-backed properties (regular and calculated) are picked up in metadata as mapped properties.
But in your case (if I understand correctly) the property is defined in the logic of the server-side class, not in the database. Therefore it is not among the mapped properties in metadata. It is hidden from metadata. It is an unmapped instance property.
I assume you're not hiding it from the serializer. If you look at the network traffic for a query of the class, you can see your calculated property data arriving at the client. The problem is that Breeze is ignoring it when it "materializes" entities from these query results.
Solution with example
The solution is to register the calculated property in the MetadataStore.
I modified the entityExtensionTests.js of the DocCode sample to include this scenario; you can get that code from GitHub or wait for the next Breeze release.
Or just follow along with the code below, starting with this snippet from the
Employee
class in NorthwindModel.cs:And here is the automated test in entityExtensionTests.js
What you need to do is in this small part of the test example: