ConventionProfile已经过时,请使用IConventionPack。

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

感谢您的选择。
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);

2
截至1.8.1.20版本,这里提供了一个库的实现:http://api.mongodb.org/csharp/current/html/28ac3635-dfe6-41bf-d29f-8fcd9c27715a.htm - Joe Waller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接