【问题标题】:Dapper Multi Mapping ResultDapper 多重映射结果
【发布时间】:2017-08-18 01:31:48
【问题描述】:

我有两节课

public class Customer
{
    public int CustomerId { get; set;}
    public string CustomerName { get; set; }
}

public class Order
{
    public int OrderId { get; set; }
    public int CustomerId { get; set; }       //BuyerCustomer
    public int CustomerSecondId { get; set; } //ReceiverCustomer
    public Customer BuyerCustomer { get; set; }
    public Customer ReceiverCustomer { get; set; }
}

我的查询如下所示

SELECT a.*, b.*, c.* FROM dbo.PRODUCTS_ORDER a
INNER JOIN  dbo.CUSTOMER b ON a.CustomerId=b.CustomerId
INNER JOIN   dbo.CUSTOMER c ON a.CustomerSecondId=b.CustomerId

Dapper 实现..

     List<Order> order= null;
 order= (List<Order>)dapperconnection.Query<Order, Customer, Customer, Order>(sql,
                                (order, customer1,customer2) =>
                                {
                                    order.BuyerCustomer = customer1;
                                    order.ReceiverCustomer = customer2;
                                    return order;
                                }, splitOn: "CustomerId,CustomerSecondId ");

我得到的结果不完整,只有 RecevierCustomer 被填充,而 BuyerCustomer 根本不包含任何值。

看起来 dapper 很困惑,因为我在查询中使用了两次 CustomerId。 是否有任何解决方法而无需更改我的 Customer 类?

【问题讨论】:

    标签: dapper


    【解决方案1】:

    您的类设计和 Dapper 查询几乎没有问题。

    • Customer.CustomerName 应该是字符串
    • 我将从订单中删除 CustomerId 和 CustomerSecondId。它们是多余的。您在 Customer 中有两个 ID。
    • 从拆分中删除 CustomerSecondId。

    下面是一个工作测试:

    public class Order
    {
        public int OrderId { get; set; }
        public Customer BuyerCustomer { get; set; }
        public Customer ReceiverCustomer { get; set; }
    }
    
    public class Customer
    {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
    }
    
    
    [Test]
    public void TestSplitMany()
    {
        using (var conn = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo"))
        {
            var result =
                conn.Query<Order, Customer, Customer, Order>(@"select OrderId = 1, CustomerId = 2, CustomerName = 'Jim', CustomerId = 3, CustomerName = 'Bob'",
                    (order, buyer, receiver) =>
                    {
                        order.BuyerCustomer = buyer;
                        order.ReceiverCustomer = receiver;
                        return order;
                    }, splitOn: "CustomerId").First();
    
            Assert.That(result.BuyerCustomer.CustomerId == 2);
            Assert.That(result.ReceiverCustomer.CustomerId == 3);
    
            Assert.That(result.BuyerCustomer.CustomerName == "Jim");
            Assert.That(result.ReceiverCustomer.CustomerName == "Bob");
        }
    }
    

    【讨论】:

    • 哦,抱歉打错了。我修好了它。让我尝试做一些测试。谢谢。
    • 为什么我需要从拆分中删除 Remove CustomerSecondId?老实说,我不明白,因为我真的需要它来连接。
    • CustomerSecondId 是订单的一部分。查看 SQL 查询结果时,您有 Order 后跟 Customer、Customer,那么我们只需将两条客户记录除以 CustomerId。我将编辑最后一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多