如何使用servicestack

时间:2016-10-27 15:11:21

标签: postgresql ormlite-servicestack

我试图只更新一个jsonb类型的列。插入工作完美没有任何意外,但我不知道如何只更新一个属性[ComplextType('json')]的字段 db.UpdateOnly(()=> new QuestionPoco(){Answers = requestDto.answers},                         其中:q => q.Id.ToString()== question.id.ToString());

1 个答案:

答案 0 :(得分:2)

现在应该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"));