Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an enum value on a dynamic Odata object

I have an ODATA service that returns a dynamic object similar to this: https://blogs.msdn.microsoft.com/leohu/2013/11/04/typeless-entity-object-support-in-webapi/

When I am populating the objects properties, I do:

 var entityCategory = new EdmEntityObject(entityType);
 bool bSuccess = entityCategory.TrySetPropertyValue("Name", "Category 1");

And this works fine, but I am unable to set the value of any of my Enum properties. Here's what I've tried:

var edmValue= new EdmEnumValue( new EdmEnumTypeReference( enumType, true ), value );
bool bSuccess = entityCategory.TrySetProperty( "Status", edmValue );

But that doesn't work and I simply get null in the response. bSuccess is true, and it even has the expected value if I call TryGetValue afterwards.

However, in the web response status still shows as NULL.

I've tried a bunch of variations, but nothing seems to work.

Any ideas what I am doing wrong?

FOUND SOLUTION

HERE is what eventually worked for me:

var oEnumObject = new EdmEnumObject( enumType, strEnumValue );
entity.TrySetPropertyValue( fieldName, oEnumObject );

Not sure why I need to use EdmEnumObject, but it appears to work. It actually will accept and display any string value for strEnumValue.

NONE OF THIS WORKS

Some more info:

When I add an enum to the datamodel I do something like:

var enumType = new EdmEnumType( Namespace, "Status", EdmPrimitiveTypeKind.Int64, false);
enumType.AddMember( "Active", 0);
..
AddProperty( EdmStructuralProperty( this, field.Name, new EdmEnumTypeReference( thisEnum, bIsNullable ) )

I cache the value of enumType here, and use it later when I am populating ( in the code above ). Therefore, I think enumType is valid. As for value, I had some code to pick the correct EdmEnumMember, but I've also just tried hard coding it to the first in the list and it still comes out as null:

var edmValue = new EdmEnumValue( new EdmEnumTypeReference( enumType, true ), enumType.Members.First() );

So what the heck? I've hit a breakpoint there and it appears to have the correct value, but I get null in the response. I've also tried this:

var value = new ODataEnumValue( nEnumIntValue.ToString(), enumType.FullName() );
entity.TrySetPropertyValue( col.ColumnName, value );

Again, NULL. Any help would be very much appreciated!

like image 980
Porco Avatar asked Oct 31 '22 03:10

Porco


1 Answers

Referencing the following test on github foe EdmEnumType

You should take a look at how you are creating and adding the members to the enumType...

//make sure you enum type is not nullable
var enumType = new EdmEnumType(Namespace, "Status", EdmCoreModel.Instance.GetInt32(false).PrimitiveDefinition(), false);

//enum members are made up of constants
var enumMember = enumType.AddMember("Active", new EdmIntegerConstant(0));

//You should also not make the value nullable
var edmValue = new EdmEnumValue(new EdmEnumTypeReference(enumType, false), enumMember);
like image 66
Nkosi Avatar answered Nov 08 '22 08:11

Nkosi