is multi mapping in dapper care about the sqlstr`s column order?

dapper orm

Question

for example:

here is my pocos:

public class product
{
    public int productid { set; get; }
    public category category { set; get; }
    public string productname { set; get; }
}

also the category:

public class category
{
    public int categoryid { set; get; }
    public string categoryname { set; get; }
    public string Description { set; get; }
}

and the sqlstr:

select p.ProductID, p.ProductName, c.CategoryName, c.CategoryID,  c.Description 
from Products p inner join Categories c 
on p.CategoryID=c.CategoryID

if i choose "c.CategoryID" as splitOn. then c.CategoryName won`t be assigned, so i changed the order of column:

select p.ProductID, p.ProductName, c.CategoryID,  c.CategoryName, c.Description ...

this time everything is ok.

am i misunderstand something, any help will be ok.

thx.

Accepted Answer

Actually, I suspect you need to use "CategoryID" to split on, not "c.CategoryID", as the results don't get to see the aliases (c.). But, to explain: the "split on" feature partitions a horizontal set of results into multiple pieces. For example, if we have:

Id    A    B    Id    C    D   Id    E    F

and we split on "Id", then we get:

Id    A    B         |      Id    C    D        |       Id    E    F

(note that the "split" can be multiple different column names; it does not need to be the same thing each time)

So: within each horizontal partition - no, the order doesn't matter. But: if the "split" columns are positioned such that a column (CategoryName) ends up in the wrong partition, then yes, that matters. This behavior is demanded by the fact that column names do not have to be unique - so it can't just look for CategoryName - that could be unrelated; it needs to look for CategoryName in the correct partition.

In your specific example: move the column, or simply use CategoryName as the split name (basically, it just needs to know the first column in the partition).



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why