我试图仅更新一列 jsonb 类型。插入工作完美,没有任何意外,但我不知道如何才能只更新一个具有属性 [ComplextType('json')] 的字段 db.UpdateOnly(() => new QuestionPoco() {Answers = requestDto.answers}, 其中: q => q.Id.ToString() == Question.id.ToString());
最佳答案
现在应该由 this commit 支持现在是available on MyGet .
我们还添加了新类型 PgSqlTypes Attributes您可以使用它来代替 [CustomField("json")]
,例如:
public class Question
{
public int Id { get; set; }
public string Text { get; set; }
//equivalent to: [CustomField("json")]
[PgSqlJson]
public List<Answer> Answers { get; set; }
}
public class Answer
{
public int Id { get; set; }
public string Text { get; set; }
}
您可以正常插入/更新,例如:
db.DropAndCreateTable<Question>();
var createTableSql = db.GetLastSql();
Assert.That(createTableSql, Does.Contain("\"answers\" json NULL"));
db.Insert(new Question
{
Id = 1,
Answers = new List<Answer>
{
new Answer { Id = 1, Text = "Q1 Answer1" }
}
});
var question = db.SingleById<Question>(1);
Assert.That(question.Answers.Count, Is.EqualTo(1));
Assert.That(question.Answers[0].Text, Is.EqualTo("Q1 Answer1"));
db.UpdateOnly(() => new Question {
Answers = new List<Answer> { new Answer { Id = 1, Text = "Q1 Answer1 Updated" } }
},
@where: q => q.Id == 1);
question = db.SingleById<Question>(1);
Assert.That(question.Answers[0].Text, Is.EqualTo("Q1 Answer1 Updated"));
关于postgresql - 如何使用 servicestack 中的 ormLite 更新复杂类型字段 (json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40288261/