Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Mongo驱动程序ReplaceOneModel在upsert上插入空id_C#_Mongodb_Mongodb .net Driver_Upsert - Fatal编程技术网

C#Mongo驱动程序ReplaceOneModel在upsert上插入空id

C#Mongo驱动程序ReplaceOneModel在upsert上插入空id,c#,mongodb,mongodb-.net-driver,upsert,C#,Mongodb,Mongodb .net Driver,Upsert,我正在尝试使用c#mongo驱动程序在.NETCore2中批量插入一个集合。我遇到的问题是_id字段总是作为null插入 代码如下: public async Task UpsertEvents(IEnumerable<Event> events) { if(events.Count()>0){ List<ReplaceOneModel<Event>> requests = new List<Re

我正在尝试使用c#mongo驱动程序在.NETCore2中批量插入一个集合。我遇到的问题是_id字段总是作为null插入

代码如下:

  public async Task UpsertEvents(IEnumerable<Event> events)
    {
        if(events.Count()>0){
            List<ReplaceOneModel<Event>> requests = new List<ReplaceOneModel<Event>>();
            foreach (var ev in events)
                {
                var filter = new FilterDefinitionBuilder<Event>().Where(m => m.Id == ev.Id);
                var request = new ReplaceOneModel<Event>(filter,ev);
                request.IsUpsert = true;
                requests.Add(request);
                }
            await _context.Events.BulkWriteAsync(requests);
        }
    }
有更多的字段,但它们在这里并不真正相关


你知道为什么要为id插入null吗?正常插入正确生成ID

使用UpdateModel并列出更新中的所有属性

List<UpdateOneModel<Event>> requests = new List<UpdateOneModel<Event>>();
var update = Builders<Event>.Update.Set("field", "value");
var request = new UpdateOneModel<Event>(filter,update);
列表请求=新列表();
var update=Builders.update.Set(“字段”、“值”);
var请求=新的UpdateModel(过滤器,更新);

请阅读“使用替换进行upsert为什么会在id中插入空值”的说明。但是,[BsonIgnoreIfDefault]应该适用于您。

我遇到了同样的问题,我使用以下方法解决了它:

ConventionRegistry.Register("IgnoreIfDefault",
            new ConventionPack { new IgnoreIfDefaultConvention(true) },
            t => true);
只需添加存储库的构造函数,一切都会正常工作。使用UpdateModel将强制您设置所有字段。。。在我的项目中,我有十几个字段,设置所有字段并不容易。现在我可以单独使用带有BulkWrite的ReplaceOneModel

var client = new MongoClient(settings.Value.ConnectionString);

var database = client.GetDatabase(settings.Value.DatabaseName);

// Add this to the constructor of your Repository and everything will work fine 

ConventionRegistry.Register("IgnoreIfDefault",
            new ConventionPack { new IgnoreIfDefaultConvention(true) },
            t => true);

快乐编码:-)

谢谢。我很可能会这样做,而且它确实有效,只是稍微多了一些,我不明白BsonIgnoreIfDefault为什么不工作。如果它对你有帮助,请考虑接受这个答案。
var client = new MongoClient(settings.Value.ConnectionString);

var database = client.GetDatabase(settings.Value.DatabaseName);

// Add this to the constructor of your Repository and everything will work fine 

ConventionRegistry.Register("IgnoreIfDefault",
            new ConventionPack { new IgnoreIfDefaultConvention(true) },
            t => true);