简体   繁体   English

Dapper 一对多关系映射

[英]Dapper one to many relationship mapping

I'm trying to map a one-to-many relationship with dapper.我正在尝试 map 与小巧玲珑的一对多关系。 But I don't get it to working.但我没有让它工作。

I have a table order and another one for the products order_product我有一个表order和另一个产品order_product

Now I have this code:现在我有这段代码:

var sql = @"SELECT 
* 
FROM `order` o
INNER JOIN `order_product` op ON op.order_id = o.order_id
WHERE o.order_id = 5153";
var products = await connection.QueryAsync<Order, OrderProduct, Order>(sql, (order, product) =>
{
    if (order.Products == null)
    {
        order.Products = new List<OrderProduct>();
    }

    if (product != null)
    {
        order.Products.Add(product);
    }

    return order;
}, splitOn: "order_id");

For testing purposes I'm loading the order with id 5153 which consists of 4 products.出于测试目的,我加载了包含 4 个产品的 ID 为 5153 的订单。

The class OrderProduct is defined as: class OrderProduct 定义为:

public class OrderProduct
    {
        public int order_product_id { get; set; }
        public int order_id { get; set; }
        public int product_id { get; set; }
        public string name { get; set; }
        public string model { get; set; }
        public int quantity { get; set; }
        public decimal price { get; set; }
        public decimal total { get; set; }
        public decimal tax { get; set; }
        public int reward { get; set; }
    }

The order class holds all properties from the order table +订单 class 包含订单表中的所有属性 +

public ICollection<OrderProduct> Products { get; set; } 

However instead of 1 Order object with 4 OrderProduct objects I get 4 Order objects where each contains one product of the 4.但是,我得到 4 个 Order 对象,而不是 1 个 Order object 和 4 个 OrderProduct 对象,其中每个对象包含 4 个中的一个产品。

Does anybody know what I did wrong here?有人知道我在这里做错了什么吗?

Try this way:试试这样:

        var sql = @"SELECT * FROM `order` o 
              INNER JOIN `order_product` op ON op.order_id = o.order_id
            WHERE o.order_id = 5153";
        var orderDictionary = new Dictionary<int, Order>();
        var products = await connection.QueryAsync<Order, OrderProduct, Order>(sql, (order, product) =>
        {
            if (!orderDictionary.TryGetValue(order.order_id, out Order docEntry))
            {
                docEntry = order;
                docEntry.Products = new List<OrderProduct>();
                orderDictionary.Add(docEntry.order_id, docEntry);
            }

            if (product != null) docEntry.Products.Add(product);
            docEntry.Products = docEntry.Products.Distinct().ToList();

            return docEntry;
        }
        , splitOn: "order_id");

you got the order List on the orderDictionary, if you only want a list, get it this way你在 orderDictionary 上得到了订单列表,如果你只想要一个列表,就这样获取

var orderList = orderDictionary.Values.ToList();

select * is a bad idea for productions queries, you need to be sure your order_id column is the splitOn one, and you have two of them, normally Dapper does his work and try to guess which one is the splitter, but bettter if you fix itby aliasing one of them select * 对于生产查询来说是个坏主意,您需要确保您的 order_id 列是 splitOn 列,并且您有两个,通常 Dapper 会完成他的工作并尝试猜测哪个是拆分器,但如果您修复会更好它通过别名其中之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM