[EF-71] Mapping of ObjectIds to strings in entities Created: 06/Nov/23  Updated: 14/Nov/23

Status: Backlog
Project: Entity Framework
Component/s: None
Affects Version/s: Public Preview 1
Fix Version/s: None

Type: New Feature Priority: Critical - P2
Reporter: Markus Wildgruber Assignee: Unassigned
Resolution: Unresolved Votes: 4
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When mapping MongoDB documents to C# classes, having string properties in the classes simplifies the handling of ObjectIds very much. In the existing driver for C#, this can be done by applying an [BsonRepresentation(BsonType.ObjectId)]-attribute to the property (also works if the property is an array or enumeration). However, this had no effect for the EF provider. 

Also, adding a conversion when defining the model did not help, but led to an exception: 

modelBuilder.Entity<Movie>()
  .ToCollection("movies")
  .Property(x => x.Id)
  .HasElementName("_id")
  .HasConversion(x => ObjectId.Parse, x => x.ToString());

Exception: 
System.FormatException: Cannot deserialize a 'String' from BsonType 'ObjectId'.
at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SealedClassSerializerBase`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
at MongoDB.Bson.Serialization.BsonSerializationInfo.DeserializeValue(BsonValue value)
at MongoDB.EntityFrameworkCore.Serializers.SerializationHelper.ReadElementValue[T](BsonDocument document, BsonSerializationInfo elementSerializationInfo)
Using strings instead of an ObjectId type also simplifies the JSON serialization of the class. ObjectId objects are serialized like this by default: 

{ "timestamp": 1463423888, "machine": 15897363, "pid": -13653, "increment": 13451573, "creationTime": "2016-05-16T18:38:08Z" }

This format is neither recognizable as an ObjectId by a human, nor can it be used as URL parameter in an API easily. So one would have to create a custom serializer for the API anyway; better yet if the EF provider supported the mapping in the same way as the existing driver for C#. 

Additional side info: creating an EDM-model for OData based upon the class also had difficulties with the id  being an ObjectId; I expect this to handle a string without a problem.

(sorry, I'm not a MongoDB employee so I had to create a bug issue for this. From my point of view, it's more of an improvement - but an important one)



 Comments   
Comment by Esha Bhargava [ 06/Nov/23 ]

m.wildgruber@sevantage.de Thank you for your suggestion. We'll consider it for a future version.

Generated at Thu Feb 08 08:26:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.