2015-05-28 41 views
0

我有一種情況,我想使用ServiceStack.OrmLiteLoadSelect來填充由多個表中的列組成的POCO。ServiceStack LoadSelect不正確<Into>參考

基本的背景是有服務請求(要完成一個現場技術)和一個單獨的分配表。對於部分應用程序,我想將ServiceRequestServiceRequestAssignment合併爲一個ServiceRequestExtended類。

下面是波蘇斯:

public class ServiceRequest 
{ 
    [AutoIncrement] 
    public int Id { get; set; } 

    ... 

} 

public class ServiceRequestAssignment 
{ 
    [AutoIncrement] 
    public int Id { get; set; } 

    [References(typeof(User))] 
    public int? UserId { get; set; } 

    [Reference] 
    public User User { get; set; } 

    [References(typeof(ServiceRequest))] 
    public int ServiceRequestId { get; set; } 

    [Reference] 
    public ServiceRequest ServiceRequest { get; set; } 

    public double RelativeOrder { get; set; } 

    [RowVersion] 
    public ulong RowVersion { get; set; } 
} 

// Basically combines all fields of ServiceRequest & ServiceRequestAssignment 
public class ServiceRequestExtended : ServiceRequest 
{ 
    [References(typeof(User))] 
    public int? UserId { get; set; } 
    [Reference] 
    public User User { get; set; } 

    public double RelativeOrder { get; set; } 
    public ulong RowVersion { get; set; } 
} 

這裏是下述(通用)查詢中使用:

var exp = Db.From<ServiceRequest>() 
      .Join<ServiceRequestAssignment>() 
      .Where<ServiceRequest, ServiceRequestAssignment>((q, r) => ...); 

var results = Db.LoadSelect<ServiceRequestExtended, ServiceRequest>(exp); 

當我做到這一點,即LoadSelect生成查詢到相關聯的表中帶來/ POCO不正確。它在子查詢中引用ServiceRequestExtended而不是正確的子表(在本例中爲ServiceRequestAssignment)。

這裏的SQL:

SELECT "Id", "UserName", "TechnicianNumber", "FirstName", "LastName", "PIN",  "Active", "Salt" 
FROM "Users" WHERE "Id" 
IN (SELECT "ServiceRequestExtended"."UserId" 
    FROM "ServiceRequest" 
    INNER JOIN "ServiceRequestAssignment" 
    ON ("ServiceRequest"."Id" = "ServiceRequestAssignment"."ServiceRequestId") 
    WHERE ...) 

所以 - 我到底做錯了什麼? LoadSelect<Into, From>有錯誤嗎?

+0

我也試過'ServiceRequestExtended.UserId'上的'BelongsTo'屬性沒有成功 – jklemmack

回答

1

OrmLite Self References只能在real表中OrmLite需要查詢現有引用字段ID以填充POCO引用。

+0

我想通過「PopulateWith」做一個手動翻譯是很容易的,或者甚至更明確地用一個新的ServiceRequestExtended( ){...}在數據被加載後。 – jklemmack