返回

c#-为什么我在使用 MongoDB 时出现 E11000 重复键错误

发布时间:2022-05-20 07:38:25 375
# flask

此 C# 应用程序需要与 MongoDB 一起使用。当我尝试添加数据时,出现错误:E11000 重复键错误集合:aap.Olas 索引:id重复键:{ : BinData(3, 00000000000000000000000000000000) }

这是将数据添加到数据库的代码

public void Add(IDataType item,string name)
{
     IMongoCollection collection = db.GetCollection(name);
     collection.InsertOne(item);
}

这就是界面的外观。

using MongoDB.Bson.Serialization.Attributes;
using System;

namespace Labo04.GLOBAL
{
    public interface IDataType
    {
        [BsonId]
        Guid Id { get; set; }
    }
}

这是Ola类,我尝试插入的数据。

public class OLA : IDataType
    {
        [BsonId]
        public Guid Id { get; set; }
        public string Code { get; set; }
        public string Naam { get; set; }
        public int Studiepunten { get; set; }
        public virtual List Docenten { get; set; }
        public virtual OPO Opo { get; set; }

        public OLA()
        {
        }

        public OLA(string code, string naam, int studiepunten, List docenten, OPO opo)
        {
            this.Code = code;
            this.Naam = naam;
            this.Studiepunten = studiepunten;
            this.Docenten = docenten;
            this.Opo = opo;
        }
    }

我怎样才能解决这个问题。

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像