3

我刚刚将我的 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);

谢谢。

4

1 回答 1

6

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

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

其次是:

var conventions = new OpusOneConvention();
ConventionRegistry.Register("IgnoreIfNull", conventions, t => true);

因此,基本上,您的所有约定都将作为 IEnumerable 进行,然后 ConventionRegistry 将负责注册它们。

谢谢。

注意:从 1.8.1.20 版本开始,您可以按如下方式使用 ConventionPack:

var conventions = new ConventionPack();
conventions.Add(new IgnoreIfNullConvention(true));
ConventionRegistry.Register("IgnoreIfNull", conventions, x => true);
于 2013-05-06T17:30:15.400 回答