Include server side properties not mapped to entity framework entity
Asked Answered
I

2

7

We are adding additional properties to our objects on the server that we do not track in the database. The data gets returned to the client but the object materialized by breeze does not have it as a property. How can we add properties to our objects that we return to breeze but not map them into the database where we store the data?

Example: Widget Class Property A - Mapped to Database Property B - Has [NotMapped] attribute so that it is not stored in the database. It is calculated on the fly by the server.

When we get the object on the client we get: Widget Class = { Property A: ko.observable(Value for A) }

Property B is missing.

When we look at the json returned by the server we see:

Widget Class = { Property A: Value for A, Property B: Value for B }

Irksome answered 23/7, 2013 at 18:12 Comment(0)
U
4

Properties with NotMapped attributes are not part of the MetaData generated by EFContextProvider, so those properties won't be available in the client side breeze entity. But in js you can extend the client side entity and add the property to the entityType yourself (with an initial value set). This will make sure that the value for the property is correctly set when the json object is retrieved from the server.

http://www.breezejs.com/documentation/extending-entities

Underwing answered 11/8, 2013 at 6:6 Comment(2)
arul is correct. This worked for me. Read the doc he shared by breeze team. In the entity type constructor you have to declare the NotMapped property with a default value using the exact same name as in your entity in the server (if it starts with upper case, follow the same). e.g.: Entity "TheEntity" has NotMapped bool property called "Not". Then in javascript: metadataStore.registerEntityTypeCtor('TheEntity', function () { this.Not = ko.observable(false); }); And that's all, it will use the value supplied in the ctor unless it comes with a different value from the Server.Zarla
This is so inconvinient! I want to have ability to add some NotMapped writable server properties to entity!Merari
A
0

This sounds like something that Ward Bell mentioned - maybe the property has a null value and by default Breeze does not return it.

Check out adding a custom breeze configuration class as mentioned here to tweak the way null values are returned in the Json. Just adding this class into your Web Api project means Breeze will look for it automatically and configure this.

http://www.breezejs.com/documentation/web-api-controller

Afore answered 24/7, 2013 at 14:16 Comment(1)
No. It's not a null value. You can see the property in the json data returned by the client to the server. Is there any way you can get access to the properties returned to the client that are not included in the entity type definition?Irksome

© 2022 - 2024 — McMap. All rights reserved.