0
我在嘗試使用Dapper
來創建對象Work
的List
以進行映射。Dapper MultiMapping不起作用
這是代碼:
public class Work
{
public int id { get; set; }
public int id_section { get; set; }
public decimal price { get; set; }
public Model id_model { get; set; }
public Type id_type { get; set; }
}
class Model
{
public int id_model { get; set; }
public string Name { get; set; }
}
class Type
{
public int id_type { get; set; }
public string Name { get; set; }
}
public List<Work> GetListOfWork(int idList)
{
using (DatabaseConnection db = new DatabaseConnection()) //return a connection to MySQL
{
var par = new {Id = idList};
const string query = "SELECT id,id_section,price,id_model,id_type FROM table WHERE id_section = @Id";
return db.con.Query<Work, Model, Type, Work>(query,
(w, m, t) =>
{
w.id_model = m;
w.id_type = t;
return w;
}, par, splitOn: "id_model,id_type").ToList();
}
}
它不給我任何錯誤,但id_model
和id_type
在我返回列表總是空的(對象被創建,但所有的字段爲空或空) ,其他字段映射正確。
任何線索?
而且怎麼可能填補任何東西,如果你不從數據庫中檢索的字段,缺少一些加入? – Steve
由於我的查詢很複雜(這只是一個例子),手動執行映射會不好嗎?就像創建一個使用Model和Type的構造函數映射ListOfWork的函數一樣? – SilentRage47