Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Attach Entity to DataServiceContext if Entity Key is an Enum #3080

Open
noahsrc opened this issue Oct 8, 2024 · 0 comments
Open

Cannot Attach Entity to DataServiceContext if Entity Key is an Enum #3080

noahsrc opened this issue Oct 8, 2024 · 0 comments
Assignees
Labels

Comments

@noahsrc
Copy link

noahsrc commented Oct 8, 2024

I am hitting an error when detaching a tracked entity from a DataServiceContext, then attaching it to the same context at a later time. This is occurring with entities that contain an enumeration type as a key. It seems enum keys are not supported for the DataServiceContext.AttachTo method.

Assemblies affected

OData.Core (.NET Framework) v7.21.4

Reproduce steps

var context = *set up context*

// Retrieve entity that contains an enumeration key. In this case, the keys are orderNo (string) and documentType (enum)
DataServiceCollection<SalesOrder> orderCollection = new DataServiceCollection<SalesOrder>(context.SalesOrders.Where(so => so.OrderNo == "S-ORD101010" && so.DocumentType == SalesDocumentType.Order));

object entity = context.Entities.First().Entity;

context.Detach(entity);

// Attaching throws exception
context.AttachTo("SalesOrders", entity);

Expected result

The entity is attached to the DataServiceContext.

Actual result

The entity is not attached to the DataServiceContext and an error is thrown:

A value with primitive kind 'Enum' cannot be converted into a primitive object value.

Additional detail

The full exception reads:

Microsoft.OData.ODataException: A value with primitive kind 'Enum' cannot be converted into a primitive object value.
   at Microsoft.OData.Client.EdmValueUtils.ToClrValue(IEdmPrimitiveValue edmValue)
   at Microsoft.OData.Client.DataServiceUrlKeyDelimiter.<>c__DisplayClass9_0`1.<AppendKeyExpression>b__0(T p)
   at Microsoft.OData.Client.KeySerializer.GetKeyValueAsString[TProperty](Func`2 getPropertyValue, TProperty property, LiteralFormatter literalFormatter)
   at Microsoft.OData.Client.KeySerializer.AppendKeyWithParentheses[TProperty](StringBuilder builder, ICollection`1 keyProperties, Func`2 getPropertyName, Func`2 getPropertyValue)
   at Microsoft.OData.Client.DataServiceUrlKeyDelimiter.AppendKeyExpression(IEdmStructuredValue entity, StringBuilder builder)
   at Microsoft.OData.Client.ConventionalODataEntityMetadataBuilder.ConventionalODataUriBuilder.BuildEntityInstanceUri(Uri baseUri, IEdmStructuredValue entityInstance)
   at Microsoft.OData.Client.DataServiceContext.AttachTo(String entitySetName, Object entity, String etag)

An additional switch case for Enums may need to be added in the ToClrValue method in Microsoft.OData.Core/Evaluation/EdmValueUtils.cs.

On initial inspection, adding this case solves the issue:

case EdmValueKind.Enum:
    return ((IEdmEnumValue)edmValue).Value.Value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants