Mongo DB错误嵌套类型初始化C#
我正在尝试在Mongo DB中创建一个新文档,但是我有2个问题。 我的代码:
internal class Doc
{
internal int ItemID { get; set; }
internal string Some { get; set; }
internal List<Slot> Slots { get; set; }
internal List<Event> Events { get; set; }
}
internal class Slot
{
internal int FirstID { get; set; }
internal int SecondID { get; set; }
internal int Role { get; set; }
}
internal class Events
{
internal int Event { get; set; }
internal int Time { get; set; }
}
internal static async void Create(int itemId, string some, int firstId, int secondId)
{
var doc = new Doc()
{
ItemID = itemId,
Some = some,
Slots =
{
new Slot()
{
FirstID = firstId,
SecondID = secondId,
Role = 2
} // NullReferenceException at here
},
Events =
{
}
};
await Mongo.MyCollection.InsertOneAsync(doc);
}
第一个问题:代码停止在我标记的注释中,并带有错误系统。NullReferenceException:“对象引用未设置为对象的实例。”。 第二个问题:即使我删除了“新插槽”,我也只有一个空的文档。
I am trying to create a new document in Mongo DB, but I have 2 problems.
My Code:
internal class Doc
{
internal int ItemID { get; set; }
internal string Some { get; set; }
internal List<Slot> Slots { get; set; }
internal List<Event> Events { get; set; }
}
internal class Slot
{
internal int FirstID { get; set; }
internal int SecondID { get; set; }
internal int Role { get; set; }
}
internal class Events
{
internal int Event { get; set; }
internal int Time { get; set; }
}
internal static async void Create(int itemId, string some, int firstId, int secondId)
{
var doc = new Doc()
{
ItemID = itemId,
Some = some,
Slots =
{
new Slot()
{
FirstID = firstId,
SecondID = secondId,
Role = 2
} // NullReferenceException at here
},
Events =
{
}
};
await Mongo.MyCollection.InsertOneAsync(doc);
}
First problem: the code stops at the line that I marked with a comment, with an error System.NullReferenceException: "Object reference not set to an instance of an object.".
Second Problem: Even if I remove "new Slot", then I just have an empty document in MongoDB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论