最佳答案
我正在尝试使用 Dapper 的多重映射特性来返回 ProductItems 和相关 Customer 的列表。
[Table("Product")]
public class ProductItem
{
public decimal ProductID { get; set; }
public string ProductName { get; set; }
public string AccountOpened { get; set; }
public Customer Customer { get; set; }
}
public class Customer
{
public decimal CustomerId { get; set; }
public string CustomerName { get; set; }
}
我的衣冠楚楚代码:
var sql = @"select * from Product p
inner join Customer c on p.CustomerId = c.CustomerId
order by p.ProductName";
var data = con.Query<ProductItem, Customer, ProductItem>(
sql,
(productItem, customer) => {
productItem.Customer = customer;
return productItem;
},
splitOn: "CustomerId,CustomerName"
);
这样做很好,但是我似乎必须将完整的列列表添加到“ splitOn”参数中,以返回所有客户的属性。如果我不添加“ CustomerName”,它将返回 null。我是否误解了多映射特性的核心功能?我不想每次都要添加一个完整的列名列表。