You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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
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:
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:
The text was updated successfully, but these errors were encountered: