我正在嘗試使用Dapper的Multimapping功能來返回ProductItem和相關客戶的列表。在Dapper中正確使用多重映射
[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。我是否理解多圖功能的核心功能?我不想每次都添加一個完整的列名稱列表。
你怎麼實際在datagridview中顯示這兩個表呢?一個小例子將不勝感激。 –