Working with BsonDocument on MongoDb.EntityFrameworkCore

I’m trying to use a dynamic object like JsonObject, BsonDocument for entity model, I’m trying to use

    v => v.ToBsonDocument(null, null, null, default),
    v => BsonSerializer.Deserialize<JsonObject>(v, null));

But it’s not working. Do we have any way for MongoDB.EntityFramework works with a dynamic object like BsonDocument, JsonObject, or JObject?

Hi Thuy.

Entity Framework is very much designed around simple POCO/Entity/DTO type objects.

BsonDocument is quite the opposite of this being a general-purpose container with all sorts of helper methods and properties - many of which throw if they are accessed when the data is not of the right type.

As such BsonDocument will not work with the EF provider. Instead we would recommend you consider using the C# Driver directly if you need truly dynamic fields.

If you just needs a few dynamic fields on top of your poco/entity then the next release should include support for having a property of Dictionary<string, string> for example which might also be of use.