在Dapper中正确使用多重映射 [英] Correct use of Multimapping in Dapper

查看:96
本文介绍了在Dapper中正确使用多重映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用dapper的Multimapping功能来返回ProductItem和相关客户的列表。

I'm trying to use the Multimapping feature of dapper to return a list of ProductItems and associated Customers.

[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。我是否错过了多重映射功能的核心功能。我不想每次都添加一个完整的列名列表。

This works fine but I seem to have to add the complete column list to the splitOn parameter to return all the customers properties. If I don't add "CustomerName" it returns null. Am I miss-understanding the core functionality of the multimapping feature. I don't want to have to add a complete list of column names each time.

推荐答案

我刚刚运行了一个运行良好的测试:

I just ran a test that works fine:

var sql = "select cast(1 as decimal) ProductId, 'a' ProductName, 'x' AccountOpened, cast(1 as decimal) CustomerId, 'name' CustomerName";

var item = connection.Query<ProductItem, Customer, ProductItem>(sql,
    (p, c) => { p.Customer = c; return p; }, splitOn: "CustomerId").First();

item.Customer.CustomerId.IsEqualTo(1);

需要将splitOn参数指定为分割点,默认为Id。如果存在多个拆分点,则需要将它们添加到以逗号分隔的列表中。

The splitOn param needs to be specified as the split point, it defaults to Id. If there are multiple split points, you will need to add them in a comma delimited list.

说您的记录集如下:


ProductID | ProductName | AccountOpened | CustomerId | CustomerName 
---------------------------------------   -------------------------

Dapper需要知道如何拆分列按此顺序分成2个对象。粗略显示,客户从 CustomerId 列开始,因此 splitOn:CustomerId

Dapper needs to know how to split the columns in this order into 2 objects. A cursory look shows that the Customer starts at the column CustomerId, hence splitOn: CustomerId.

如果由于某些原因导致基础表中的列顺序被翻转,则这里有一个 big 警告:

There is a big caveat here, if the column ordering in the underlying table is flipped for some reason:


ProductID | ProductName | AccountOpened | CustomerName | CustomerId  
---------------------------------------   -------------------------

splitOn:CustomerId 将导致客户名称为空。

splitOn: CustomerId will result in a null customer name.

如果您指定 CustomerId,CustomerName 作为拆分点,则dapper会假设您尝试将结果集拆分为3个对象。第一个从开头开始,第二个从 CustomerId 开始,第三个从 CustomerName 开始。

If you specify CustomerId,CustomerName as split points, dapper assumes you are trying to split up the result set into 3 objects. First starts at the beginning, second starts at CustomerId, third at CustomerName.

这篇关于在Dapper中正确使用多重映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆