Saving JSON via EFCore Provider

I’m using the EFCore provider (v8.2.3) to interact with Mongo. Something I can’t seem to figure out is how to save arbitrary JSON as a property of a document. I’ve tried BsonDocument and Dictionary<string, object> both fail with a message like the following:

‘Dictionary<string, object>’ property ‘MyEntity’ could not be mapped because the database provider does not support this type.

This seems like it should work (Dictionary<object, string> anyway) because it looks like the serializer supports any Dictionary<,>.

Dictionary<string, string> does work but is not ideal since it only works for flat JSON (no nesting) and turns all values into strings.

Is there a way to accomplish what I am looking to do? I know I could store the JSON as a serialized string (and deserialize on read) but I’d prefer the JSON to be kept intact in Mongo.

Thanks in advance.

Unfortunately storing objects in this way is not supported as how any atribtratry object could be stored is well outside the scope of traditional EF mapping where types are known in advance and their mappings are configured.

That’s a fair point. However, the ability to store arbitrary objects is one of the great strengths of Mongo. In my opinion, not supporting this makes the EF Core provider less appealing and forces adoption of relational model, at which point we may as well just use SQL.

EF Core limits what we can do in our EF Core provider.

You could consider using our C# Driver package which allows you a lot more flexibility in the mappings while still coming with a full LINQ provider for querying.