Tell me what am I doing wrong in the request

vitaminchik 486 Reputation points
2023-05-06T20:39:33.2033333+00:00

I am using Dapper. The code is giving me an error in place of SplitOn. Error:"Multi-map error: splitOn column 'CustomerId' was not found - please ensure your splitOn parameter is set and in the correct order Arg_ParamName_Name".

I checked the request itself in pgAdmin, everything works, but why is there an error here?

public class Products
    {
        public  int Id { get; set; }
        public string? NameOfProduct { get; set; }
        public string? Description { get; set;}
        public int Quantity { get; set; }
        public decimal Price { get; set; }
    }
public class Orders
    {
        public int Id { get; set; }
        public int CustomerId { get; set; }
        public int ProdactId { get; set; }
        public int Quantity { get; set; }


    }
 public class Customers
    {
        public int Id { get; set; }
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public int Age { get; set; }
        public Products? Product { get; set; }
        public Orders? Order { get; set; }
    }
public List <Customers> AllJoin()
{
            using (var con = new NpgsqlConnection(_connectionString))
            {
                con.Open();
                string query = "SELECT o.CustomerId, c.FirstName, c.LastName, o.ProdactId,p.StockQuantity, p.Price FROM Orders AS o JOIN Customers AS c ON o.CustomerId=c.Id JOIN Products AS p ON o.ProdactId=p.Id WHERE c.Age>20 AND o.ProdactId=1";
                
                var customer=con.Query<Customers,Orders,Products,Customers>(query,(customer, order, product)=>
                {
                    customer.Order=order;
                    customer.Product=product;
                    return customer;
                },splitOn: "CustomerId, ProdactId").ToList();
                return customer;
            }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,552 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-05-08T06:46:16.3466667+00:00

    Hi,@vitaminchik. Welcome Microsoft Q&A.

    It seems that the issue might be with the splitOn parameter. In the Query method, where you use three tables (Orders, Customers, and Products), you could specify the columns on which to split the result set. The splitOn parameter tells Dapper which columns to split on.

    You could try changing the splitOn parameter to "CustomerId,ProductId,FirstName,StockQuantity" to include the ProductId column to see if that helps.

      var orders = con.Query<Order, Customer, Product, Order>(query, (order, customer, product) =>
                    {
                        order.Customer = customer;
                        order.Product = product;
                        return order;
                    }, splitOn: "CustomerId,ProdactId,FirstName,StockQuantity").ToList();
      return orders;
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.