ConventionProfile是过时的使用,而不是使用

Los*_*ost 3 c# mongodb mongodb-.net-driver

我刚刚将我的Mongo-C#驱动程序从1.6.1更新到1.8.1,我意识到它们已经使许多功能过时了.由于弃用而我看到的错误之一是:

Convention Profile已经过时,请用IConventionsPack替换它.

现在,问题是没有太多关于IConeventionPack的文档或如何使用它.我发布了一个小代码片段,任何人都可以建议如何使用IConventionPack处理这个问题?

var conventions = new ConventionProfile();
           conventions.SetIgnoreIfNullConvention(new AlwaysIgnoreIfNullConvention());
           BsonClassMap.RegisterConventions(conventions, t => true);
Run Code Online (Sandbox Code Playgroud)

谢谢.

Los*_*ost 6

好吧,事实证明,IConventionPack没有库提供的实现.我不得不手写IConventionPack的一个实现.这是代码示例:

public class OpusOneConvention : IConventionPack
{
    public IEnumerable<IConvention> Conventions
    {
        get { return new List<IConvention> { new IgnoreIfNullConvention(true) }; }
    }
}
Run Code Online (Sandbox Code Playgroud)

其次是:

var conventions = new OpusOneConvention();
ConventionRegistry.Register("IgnoreIfNull", conventions, t => true);
Run Code Online (Sandbox Code Playgroud)

基本上,所有的约定都将作为IEnumerable,然后ConventionRegistry将负责注册它们.

谢谢.

注意:从版本1.8.1.20开始,您可以使用ConventionPack,如下所示:

var conventions = new ConventionPack();
conventions.Add(new IgnoreIfNullConvention(true));
ConventionRegistry.Register("IgnoreIfNull", conventions, x => true);
Run Code Online (Sandbox Code Playgroud)

  • 从1.8.1.20开始,这里有一个库提供的实现:http://api.mongodb.org/csharp/current/html/28ac3635-dfe6-41bf-d29f-8fcd9c27715a.htm (2认同)