MongoDB C# Driver - Ignore fields on binding

后端 未结 3 1938
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 21:10

When using a FindOne() using MongoDB and C#, is there a way to ignore fields not found in the object?

EG, example model.

public class UserModel
{
           


        
3条回答
  •  余生分开走
    2020-12-04 21:42

    Yet Another possible solution, is to register a convention for this.

    This way, we do not have to annotate all classes with [BsonIgnoreExtraElements].

    Somewhere when creating the mongo client, setup the following:

            var pack = new ConventionPack();
            pack.Add(new IgnoreExtraElementsConvention(true));
            ConventionRegistry.Register("My Solution Conventions", pack, t => true);
    

提交回复
热议问题