嵌套多重映射 Dapper 分页查询中的重复字段名称问题

发布于 2024-12-05 02:19:29 字数 1169 浏览 0 评论 0原文

我在尝试使用 Dapper 进行分页查询的多重映射时遇到了问题。

因为我在此分页场景中使用嵌套查询,所以嵌套查询中有多个表,我必须连接这些表才能获取多重映射数据,但其中一些表将共享一些同名字段,您可以在下面是我的示例查询(例如 iddisplaynameemail):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-12-12 02:19:30

我可以想到两个选项:

选项 1:连接回嵌套查询之外的扩展属性:

select s.*, t1.*, t2.* from 
(
select s.*, ROW_NUMBER() OVER (order by somecol) AS RowNumber from Something s
) as X 
left join Table t1 on Id = x.SomeId
left join Table t2 on Id = x.SomeOtherId

选项 2:扩展 SqlBuilder 来处理列别名:

select s.*, /**unalias(Table,t1)**/, /**unalias(Table,t2)**/ from 
        (
        select s.*, /**alias(Table,t1)**/, /**alias(Table,t2)**/ ROW_NUMBER() OVER (order by somecol) AS RowNumber from Something s
        left join Table t1 on Id = x.SomeId
        left join Table t2 on Id = x.SomeOtherId
        ) as X 

然后定义别名宏,以使用 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:

select s.*, t1.*, t2.* from 
(
select s.*, ROW_NUMBER() OVER (order by somecol) AS RowNumber from Something s
) as X 
left join Table t1 on Id = x.SomeId
left join Table t2 on Id = x.SomeOtherId

option 2: Extend SqlBuilder to handle column aliasing:

select s.*, /**unalias(Table,t1)**/, /**unalias(Table,t2)**/ from 
        (
        select s.*, /**alias(Table,t1)**/, /**alias(Table,t2)**/ ROW_NUMBER() OVER (order by somecol) AS RowNumber from Something s
        left join Table t1 on Id = x.SomeId
        left join Table t2 on Id = x.SomeOtherId
        ) as X 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文