2017-01-30 36 views
2

我有一些使用Dapper將SQL SELECT映射到對象的繼承代碼。 SELECT具有多個具有相同名稱的列(爲簡潔起見,省略了一些列)。Dapper如何在2列之間選擇相同的名稱

SELECT 
    created_timestamp AS CreatedDate, 
    imported_timestamp AS CreatedDate 
FROM Orders 
WHERE OrderId = @OrderId 

對數據的分析顯示,每個記錄只填充2個CreatedDate列中的一個。運行一些測試顯示Dapper似乎在挑選非空的CreatedDate。我找不到有關Dapper如何處理這種情況的任何文檔。我可以依靠Dapper總是選擇非NULL值嗎?

+0

爲什麼你有2列有相同的別名?你用什麼來使用它? – Shyju

回答

0

Dapper是(微)ORM,它應該用於數據庫CRUD操作。

也就是說,你的業務邏輯應該去其他地方。實現非常簡單。不要創建重名的列。使用dapper從數據庫獲取數據,並在其他地方應用您的業務邏輯,如檢查null或其他地方。

//Following is query 
SELECT 
    created_timestamp AS CreatedDate, 
    imported_timestamp AS ImportedDate 
FROM Orders 
WHERE OrderId = @OrderId 

//Following is your POCO/DTO 
public class Order 
{ 
    public int OrderId;//or Guid if that suits you 
    public string CreatedDate;//or DateTime if that suits you 
    public string ImportedDate;//or DateTime if that suits you 
} 

//Following is your business logic 
Order order = orderService.GetOrder(orderId); 
if(order.CreatedDate != null) 
    //Do something 
else if(order.ImportedDate != null) 
    //Do something else 

根據您的研究,即使非空列由Dapper選擇;未來版本可能無法保證。

相關問題