How to create an empty ODataEntry from meta data (IEdmModel)?

Topics: OData Library
Oct 2, 2012 at 12:27 PM


I am using OData library to implement kind of dynamic proxy behavior.

Is there some built-in method that creates an empty instance of ODataEntry from the metadata model?

I am thinking of something like:

IEdmModel model;  // metadata model instance

IEdmEntityType elementType;  // metadata for entity type i want to use

ODataEntry newEntry = model.CreateNewEntryFrom(elementType)

The result is an ODataEntry instance populated with the properties for that element type with all values set to defaults (zero or null).

I need this method in order to create an intitial empty ODataEntry that I can use as data context in a detail form and then later POST to the data service using ODataMessageWriter framework.

If there is no such method:

How can I achieve it by using the public interfaces of well-known namespaces: Microsoft.Data.OData, Microsoft.Data.Edm

The (ideal) goal is to use the public API of ODataLib, without touching its source code.

Any help would be appreciated.

 Regards, Gianni

Oct 3, 2012 at 2:55 PM


There's no built-in functionality like this available today.

On the other hand, as per OData rules, you don't need to specify all the properties inside the POST request. If a property is missing from the POST, the server is required to assign a default value to it. Note though, that it's up to the server which default it uses, it doesn't have to be zero/null. But then again, the server probably knows better what is a good default.

If you still need this, it should be pretty easy to write yourself. Call the IEdmEntityType.StructuralProperties extension method, which will return a list of all structural properties on that entity (all properties except navigation properties). Then create a new ODataProperty for each of them and figure out the default value based on the property type.