- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 MongoDB C# 驱动程序与 Mongo Atlas 实例通信。我正在重构一些文档的架构,我想使用 ISupportInitilize 来读取一些额外的元素并将它们转换为新的预期架构。
这是旧文档定义:
public class ImageDocument : DocumentBase, ISupportInitialize
{
[BsonExtraElements]
public Dictionary<string, object> ExtraElements;
//Other elements omitted for brevity.
public string AzureImageId { get; set; }
public string AzureImageUrl { get; set; }
public void BeginInit()
{
}
public void EndInit()
{
}
}
这是新的文档定义:
public class ImageDocument : DocumentBase, ISupportInitialize
{
[BsonExtraElements]
public Dictionary<string, object> ExtraElements;
//Other elements omitted for brevity
public AzureImageInformationPage Original { get; set; } //Original, as uploaded
public void BeginInit()
{
}
public void EndInit()
{
if (Original == null)
{
Original = new AzureImageInformationPage {
AzureImageId = ExtraElements.GetValueOrDefault("AzureImageId").ToString(),
ImageUrl = ExtraElements.GetValueOrDefault("ImageUrl").ToString()
};
}
}
}
现在,出于某种原因 EndInit 方法从未被调用,即使 MongoDB 文档声明它应该发生 automagically .
我正在使用以下代码与 MongoDB C# 驱动程序交互:
public async Task<IList<T>> RetrieveAll<T>() where T : DocumentBase
{
return await GetCollection<T>().AsQueryable().ToListAsync();
}
public async Task<IList<T>> RetrieveWhere<T>(Expression<Func<T, bool>> query) where T : DocumentBase
{
return await GetCollection<T>().AsQueryable().Where(query).ToListAsync();
}
public async Task<T> RetrieveSingle<T>(Expression<Func<T, bool>> query) where T : DocumentBase
{
return await GetCollection<T>().AsQueryable().SingleOrDefaultAsync(query);
}
private IMongoCollection<T> GetCollection<T>() where T : DocumentBase
{
//Slightly modified from the real code, so it's easy to read.
var collectionName = typeof(T).Name.Replace("Document", string.Empty);
//Database name is hardcoded for now.
var database = mongoClient.GetDatabase("MyDb");
return database.GetCollection<T>(collectionName);
}
如何让 MongoDB 驱动程序调用 ISupportInitialize 方法?在此先感谢您帮助我。
最佳答案
我发现了问题。
截至撰写本文时,仅在针对 .NET 4.5 进行编译时才支持初始化。我正在使用 .NET 核心 2.0。
参见 this issue在 MongoDB Jira 上,以及 BsonClassMapSerializer 中的第 131 到 150 行类。
希望 MongoDB 团队尽快在 .NET Core 中添加对序列化的支持。
关于c# - MongoDB C# - 不调用 ISupportInitialize 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094673/
我正在将一个 dll 反编译为 C#。反编译器生成的项目之一包含 1000 多行错误行,如下所示: this.pictureBox1.BeginInit(); Error 385 'System.Wi
我在 ISupportInitialize 上卡住了。 我们使用继承自 System.Windows.Form.BindingSource 的自定义类。现在我们需要增强继承类的 ISupportIni
我正在使用 Newtonsoft json.net 序列化器将我的一些模型序列化为 json。其中一些很复杂,包含循环引用。由于循环引用不可序列化,因此需要通过设置 ReferenceLoopHand
我正在使用 MongoDB C# 驱动程序与 Mongo Atlas 实例通信。我正在重构一些文档的架构,我想使用 ISupportInitilize 来读取一些额外的元素并将它们转换为新的预期架构。
有人可以解释一下使用 ISupportInitialize 接口(interface)方法(BeginInit/EndInit)和使用 SuspendLayout/ResumeLayout 之间的区别
在 VS2010 中,我有一个针对 .NET Framework 4.0 的项目,然后不得不恢复到目标 v. 3.5。一旦发生这种情况,我拥有的 SplitContainer 对象将不会显示并且实际上
我是一名优秀的程序员,十分优秀!