首页 > 解决方案 > Firestore 使用 C# 在 WPF 中写入批处理?

问题描述

我正在使用google-cloud-dotnet库在我的WPF程序中使用C#使用Firestore

我需要一次写大量文档,大约2000个文档..所以我必须找到一种比每次写一个文档更好的方法

找到WriteBatch类,并尝试以下方法:

var batch = new WriteBatch();

DocumentReference docRef = db.Document("cities/myCity");
batch = batch.Set(docRef, new {
    Name = "Los Angeles",
    Country = "USA",
    State = "CA",
});

batch.CommitAsync();

无法正确启动批处理,出现错误:

WriteBatch不包含采用 0 个参数的构造函数

那么您能否解释一下我应该如何启动稍后使用的批量写入?

标签: c#wpfgoogle-cloud-firestore

解决方案


经过一番研究,我能够batch成功使用 write,例如

using Google.Cloud.Firestore;

// ...

System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "myCredentials.json");
FirestoreDb db = await FirestoreDb.CreateAsync("myProject");

var batch = db.StartBatch();

DocumentReference docRef = db.Document("cities/myCity");
batch = batch.Set(docRef, new {
    Name = "Los Angeles",
    Country = "USA",
    State = "CA",
});

batch.CommitAsync();

推荐阅读