2013-04-30 71 views
3

我刚刚将我的Mongo-C#驱动程序从1.6.1更新到1.8.1,我意识到他们已经使很多功能过时了。 ,我看到,由于对弃用的错误之一是以下几点:ConventionProfile是过时使用IConventionPack而不是

会议资料已经过时,请 IConventionsPack更换。

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

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

谢谢。

回答

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 /电流/ HTML/28ac3635-dfe6-41bf-d29f-8fcd9c27715a.htm – 2013-06-18 03:41:37

相关问题