NHibernate - 执行 SQL 来填充 DTO [英] NHibernate - Execute SQL to populate DTO
问题描述
我有一些报告实例,其中执行 sprocs 比复杂的 QueryOver 语句更容易和简单.
I have some instances for reporting where executing sprocs is easier and simpler than complicated QueryOver statements.
我有一个 DTO,而不是实体,它表示从查询返回的数据,并希望将查询结果填充到 DTO 中.我正在使用命名查询和 session.GetNamedQuery() 来执行查询.
I have a DTO, not an entity, that represents the data returned from the query and want to populate the results of the query into the DTO. I am using named queries and session.GetNamedQuery() to execute the query.
- 我是否必须为 DTO 创建映射文件?
- 如果是这样,是否可以让 NHibernate/FluentNHibernate 知道它不应该为 DTO 创建表?我的单元测试使用 NH 的 SchemaExport 工具删除和创建模式,不想为 DTO 创建表
请注意,我不想使用 Projections 和 AliasToBean 来投影 QueryOver/Linq 查询 - 我需要执行存储过程.
Please note I don't want to project a QueryOver/Linq query using Projections and AliasToBean - I need to execute the stored procedure.
干杯
推荐答案
使用 CreateSQLQuery,以下内容无需任何映射文件即可工作.也许您可以尝试使用命名查询:
With CreateSQLQuery, the following would work without any mapping file. Maybe you can give it a try with named queries :
public class YourDto
{
public int YourDtoId { get; set; }
public string YourDtoTitle { get; set; }
}
然后
var result = yourNhSession
.CreateSQLQuery("select yourColumn1 as YourDtoId, yourColumn2 as YourDtoTitle from YOUR_TABLE")
.SetResultTransformer(Transformers.AliasToBean<YourDto>())
.List<YourDto>();
这篇关于NHibernate - 执行 SQL 来填充 DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!