嵌套多重映射 Dapper 分页查询中的重复字段名称问题
我在尝试使用 Dapper 进行分页查询的多重映射时遇到了问题。
因为我在此分页场景中使用嵌套查询,所以嵌套查询中有多个表,我必须连接这些表才能获取多重映射数据,但其中一些表将共享一些同名字段,您可以在下面是我的示例查询(例如 id
、displayname
和 email
):
q = @"select * from (select p.id, p.title, p.etc...,
u1.id, u1.displayname, u1.email,
u2.id, u2.displayname, u2.email,
t.id, t.name,
row_number() over (order by " + sort.ToPostSortSqlClause() + ") as rownum" +
" from posts p" +
" join users u1 on p.owneruserid = u1.id" +
" join users u2 on p.lastediteduserid = u2.id" +
" join topics t on p.topicid = t.id" +
") seq where seq.rownum between @pLower and @pUpper";
在上面的示例中,您可以看到在嵌套查询中,有字段出现问题id
(出现在 posts
表、users
表连接和 topics
表连接中),以及 < code>displayname 和 email
(出现在两个 users
表连接中)。
到目前为止,我想到的唯一解决方法是将每个“问题”字段转换为不同的名称,但这涉及在受影响的模型中创建虚拟属性的非常混乱的过程,因此多重映射可以映射到这些“问题”字段,并编辑如果尚未设置实际值,我的模型中的“真实”属性也会检查虚拟属性的值。
另外,在上述场景中,我必须创建 x 个虚拟属性,其中 x 是查询中同一个表上可能拥有的联接数(在本例中,同一个 Users 表上有 2 个联接,因此需要 2 个唯一命名的虚拟属性)属性仅用于 Dapper 映射目的)。
这显然并不理想,而且我确信当我创建更多这样的多映射分页查询时,会出现问题并且更加混乱。
我希望有一个好的、干净的解决方案来解决这个问题?
I've ran into an issue when trying to do multi-mapping using Dapper, for pagination queries.
Because I am using a nested query in this pagination scenario, there are multiple tables within the nested query that I must join to get my multi-mapped data, but some of these tables will share some fields of the same name which you can see in my example query below (e.g. id
, displayname
and email
):
q = @"select * from (select p.id, p.title, p.etc...,
u1.id, u1.displayname, u1.email,
u2.id, u2.displayname, u2.email,
t.id, t.name,
row_number() over (order by " + sort.ToPostSortSqlClause() + ") as rownum" +
" from posts p" +
" join users u1 on p.owneruserid = u1.id" +
" join users u2 on p.lastediteduserid = u2.id" +
" join topics t on p.topicid = t.id" +
") seq where seq.rownum between @pLower and @pUpper";
In the example above you can see that within the nested query, there are going to be problems with the fields id
(appears in the posts
table, both users
table joins and the topics
table join), and also displayname
and email
(appear in both users
table joins).
The only workaround I have thought of so far involves casting each of these 'problem' fields as a different name, but this then involves the very messy process of creating dummy properties in the affected models, so multimapping can map into these, and editing the 'real' properties in my models to also check the dummy property for a value if the real value has not been set.
Also, in the above scenario I would have to create x dummy properties where x is the number of joins I may have on the same table within a query (in this example, 2 joins on the same Users table, therefore requiring 2 uniquely named dummy properties just for Dapper mapping purposes).
This is obviously not ideal and I'm sure would have knock on problems and more untidyness as I created more of these multi-mapping pagination queries.
I'm hoping there is nice, clean solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以想到两个选项:
选项 1:连接回嵌套查询之外的扩展属性:
选项 2:扩展 SqlBuilder 来处理列别名:
然后定义别名宏,以使用 INFORMATION_SCHEMA.COLUMNS 查询和缓存数据库中的列列表,并为每列添加一个“column as column_t1”字符串。
Unalias 可以非常简单地执行相反的操作。
There are 2 options I can think of:
option 1: join back to your extended properties outside of your nested query:
option 2: Extend SqlBuilder to handle column aliasing:
Then define the alias macro to query and cache a list of columns from the db using
INFORMATION_SCHEMA.COLUMNS
and simply add a 'column as column_t1` string for each column.Unalias can do the reverse quite simply.