为什么我在MongoDB上收到E11000重复密钥错误

时间:2019-05-19 16:50:32

标签: c# mongodb

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

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

public void Add(IDataType item,string name)
{
     IMongoCollection<IDataType> collection = db.GetCollection<IDataType>(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<Docent> Docenten { get; set; }
        public virtual OPO Opo { get; set; }

        public OLA()
        {
        }

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

我该如何解决。

0 个答案:

没有答案
相关问题