【问题标题】:Can I do an UPDATE on a JOIN query with OrmLite on ServiceStack?我可以在 ServiceStack 上使用 OrmLite 对 JOIN 查询进行更新吗?
【发布时间】:2017-04-07 06:18:10
【问题描述】:

我想根据包含联接的查询的结果对表中的特定字段进行更新。将 OrmLite 与 ServiceStack 结合使用。

我的班级如下:

public class Document
{
    public int Id { get; set; }
    public string BCL_Code { get; set; }
    public bool IsActive { get; set; }
    public int DocumentTypeId { get; set; }
}

public class DocumentType
{
    public int Id { get; set; }
    public string TypeName { get; set; }
}

尝试使用以下代码对加入进行更新:

var q = db.From<Document>()
    .Join<Document, DocumentType>(
        (doc, type) => doc.DocumentTypeId == type.Id)
    .Where(d => d.BCL_Code == "FR49")
    .And<DocumentType>(t => t.TypeName == "Enrollment Process");

db.UpdateOnly(new Document { IsActive = false }, onlyFields: q);

我知道我可以更新特定字段,并且我知道如何进行联接,但是当我尝试在查询中包含联接,然后执行 UpdateOnly 时,我在 db.UpdateOnly 行上收到一条错误消息:

无法绑定多部分标识符“DocumentType.TypeName”。

是否可以对加入查询进行更新? 如果是这样,正确的做法是什么?

【问题讨论】:

    标签: ormlite-servicestack


    【解决方案1】:

    OrmLite 中还没有用于从表中更新的 Typed API,您可以 add a feature request 获取它。

    同时您可以使用自定义 SQL,例如:

    db.ExecuteSql(@"UPDATE Document SET IsActive = @isActive
      FROM Document d 
           INNER JOIN DocumentType t ON (d.DocumentTypeId = t.Id)
      WHERE d.BCL_Code = 'FR49'
        AND t.TypeName = 'Enrollment Process'", 
      new { isActive = false });
    

    【讨论】:

    • 谢谢,神话。感谢您的快速响应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多