convonprofile已过时,请使用iconconvonpack代替

本文关键字:iconconvonpack 代替 请使用 过时 convonprofile | 更新日期: 2023-09-27 18:11:40

我刚刚将我的mongo - c#驱动程序从1.6.1更新到1.8.1,我意识到他们已经淘汰了很多功能。由于弃用,我看到的错误之一如下:

Convention Profile已过时,请替换为IConventionsPack .

现在,问题是没有太多关于IConeventionPack或如何使用它的文档。我张贴了一个小代码片段,谁能建议如何处理这个使用iconconventionpack ?

var conventions = new ConventionProfile();
           conventions.SetIgnoreIfNullConvention(new AlwaysIgnoreIfNullConvention());
           BsonClassMap.RegisterConventions(conventions, t => true);

谢谢。

convonprofile已过时,请使用iconconvonpack代替

嗯,事实证明,没有库提供的iconconvonpack实现。我必须手写一个iconconventionpack的实现。下面是代码示例:

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);