c#Firesharp-如何更新对象的单个值?
我有一个c#项目,我正在尝试使用更新对象的单个值。但是,当我这样做时,它会删除其他字符串对象 tldr问题: 是否可以使用Firesharp仅更新对象的单个字段,或者在更新时必须包含所有字段 在GitHub上的示例中,他们将所有字段设置为:c#Firesharp-如何更新对象的单个值?,c#,firebase,firebase-realtime-database,fire-sharp,C#,Firebase,Firebase Realtime Database,Fire Sharp,我有一个c#项目,我正在尝试使用更新对象的单个值。但是,当我这样做时,它会删除其他字符串对象 tldr问题: 是否可以使用Firesharp仅更新对象的单个字段,或者在更新时必须包含所有字段 在GitHub上的示例中,他们将所有字段设置为: var todo = new Todo { name = "Execute SET", priority = 2 }; SetResponse res
var todo = new Todo {
name = "Execute SET",
priority = 2
};
SetResponse response = await _client.SetAsync("todos/set", todo);
Todo result = response.ResultAs<Todo>(); //The response will contain the data written
当我上传到firebase时,我使用:
StudentProfile studentProfile = new StudentProfile
{
Firstname = "John",
Lastname = "Doe",
Age = 18
};
client.Set("Students/" + firebaseUserId + "/StudentProfile", studentProfile);
现在让我们假设我想更新年龄。首先,我会得到以下信息:
var result = client.Get("Students/" + firebaseUserId + "/StudentProfile");
StudentProfile student = result.ResultAs<StudentProfile>();
是否可以使用Firesharp仅更新对象的单个字段,或者在更新时必须包含所有字段
基本上,如果每次我想更新某些内容时都必须一致地写出所有字段,那将是一种痛苦。最简单的方法是将左侧的路径扩展到age,然后使用
SetAsync
:
client.GetAsync(
"Students/" + firebaseUserId + "/StudentProfile/Age",
newAge
)
我会尝试github讨论页面:@Jazb,我以前从未知道它的存在。谢谢你指出这一点。我也会在那里试试。
var result = client.Get("Students/" + firebaseUserId + "/StudentProfile");
StudentProfile student = result.ResultAs<StudentProfile>();
int newAge = student.Age + 1;
StudentProfile updatedStudent = new StudentProfile
{
Age = newAge
};
client.UpdateAsync("Students/" + firebaseUserId + "/StudentProfile, updatedStudent)
client.GetAsync(
"Students/" + firebaseUserId + "/StudentProfile/Age",
newAge
)