postgresql - 如何使用 servicestack 中的 ormLite 更新复杂类型字段 (json)

标签 postgresql ormlite-servicestack

我试图仅更新一列 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/

相关文章:

postgresql - 如何在 PostgreSQL 中查找字符串中特定字符的第一次和最后一次出现

ormlite-servicestack - 如何在ServiceStack OrmLite中联接2个表并选择两个类?

c# - ServiceStack返回响应语法错误

java - 2019年客户端-服务器之间的高效数据库同步

c# - Ormlite 不会将日期时间从 SQL Server 映射到 C#?

servicestack - 使用参数调用存储过程时出现 SqlScalar<T> 和 SqlList<T> 问题

ormlite-servicestack - ServiceStack OrmLite,未知模块中发生类型 'System.NullReferenceException' 的第一次机会异常

sql - Postgresql:将列拆分为行

python - django.db.migrations.RenameModel 和 AutoField 序列名称

postgresql - 在 hibernate 中获取具有时区值的 itimestamp