Mongo DB错误嵌套类型初始化C#

发布于 2025-01-22 03:29:02 字数 1484 浏览 5 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文