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)
谢谢.
好吧,事实证明,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)
| 归档时间: |
|
| 查看次数: |
577 次 |
| 最近记录: |