【问题标题】:How to update complex type field (json) using ormLite from servicestack如何使用 servicestack 中的 ormLite 更新复杂类型字段 (json)
【发布时间】:2016-10-27 15:11:20
【问题描述】:

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

【问题讨论】:

    标签: postgresql ormlite-servicestack


    【解决方案1】:

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

    【讨论】:

    • 任何想法何时可用于 nuget 和 .net 核心?
    猜你喜欢
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多