Dapper 是否会自动选择与属性匹配的列?
使用 Dapper-dot-net 时,如果您查询强类型结果,并且您的 SQL 只有一个:
select *
Dapper 会自动仅对与对象中的字段匹配的列进行选择吗?我认为 PetaPOCO 做到了这一点,但我遇到了一些关于 dapper 的问题,我认为这些问题归因于这种不匹配。
例如,
conn.Query<article>("select * from Article");
如果 Article
表包含与 article
对象无关的其他列,这是否有效?
When using Dapper-dot-net, if your querying to a strongly typed results, and your SQL just has a:
select *
Will Dapper automappically only do a select on the columns that match the fields in your object? I think PetaPOCO does this but I ran into some problems with dapper that I thought were attributed to this mismatch.
Example,
conn.Query<article>("select * from Article");
Will this work if the Article
table contains other columns that are extraneous to the article
object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,确实如此 - 我在周末尝试过这个,即使查询涉及由 FK 约束连接的两个表。我创建了两个类,它们仅代表这些基础表的一部分,并且存在的那些属性将被很好地填充,类中不存在的任何内容都将被忽略。效果就像一个魅力!
另一方面:如果您只需要几列 - 您确实应该在 SQL 查询中明确指定这些列 - 作为一般最佳实践!如果您只需要少数列,那么选择所有内容都是没有意义的......
Yes it does - I tried this over the weekend, even with a query involved two tables joined by a FK constraint. I created two classes that represented only parts of those underlying tables, and those properties present will be filled just fine, anything that's not in the classes will be ignored. Works like a charm!
On the other hand: if you only need a few column - you should really specify those explicitly in your SQL query - as a general best practice! No point in selecting everything, if you need only a handful of columns....