如何阻止 AutoMapper 使用 NHibernate 生成两个相同的 SELECT 语句?
我有一个名为 Incident
的实体和一个名为 IncidentDTO
的 DTO。目前,IncidentDTO
看起来像这样:
public class IncidentDTO : Incident
{
// empty until I can get AutoMapper working correctly
}
我正在尝试提取数据库中所有Incidents
的列表,并使用以下代码将它们转换为 DTO:
Mapper.CreateMap<Incident, IncidentDTO>();
using (var session = SessionFactory.OpenSession())
{
var incidents = session.Linq<Incident>();
var incidentDTOs = Mapper.Map(incidents, new List<IncidentDTO>());
}
此代码有效很好,除了当我使用 NProf 查看生成的 SQL 语句时,我得到以下结果:
SELECT ... FROM [Incident] this_
SELECT ... FROM [Incident] this_
这两个 SELECT 语句完全相同。为什么 AutoMapper 会生成两个相同的 SELECT 语句,以及如何防止它这样做?
I have an entity called Incident
and a DTO called IncidentDTO
. For now, IncidentDTO
simply looks like this:
public class IncidentDTO : Incident
{
// empty until I can get AutoMapper working correctly
}
I'm trying to pull a list of all the Incidents
in the database and convert them to DTOs using this code:
Mapper.CreateMap<Incident, IncidentDTO>();
using (var session = SessionFactory.OpenSession())
{
var incidents = session.Linq<Incident>();
var incidentDTOs = Mapper.Map(incidents, new List<IncidentDTO>());
}
This code works fine, except when I use NHProf to look at the SQL statements being generated, I get this:
SELECT ... FROM [Incident] this_
SELECT ... FROM [Incident] this_
The two SELECT statements are exactly identical. Why does AutoMapper generate two identical SELECT statements, and how do I prevent it from doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
猜测:枚举 IQueryable 会为每个项目创建一个单独的选择。通过枚举IList来解决。
我可能会这样做是为了防止另一个问题。
这只是一个猜测,并不是我真正知道的。
A guess: Enumerating IQueryable creates a separate select for every item. Solve it with enumerating IList.
I would probably do this to prevent another problem.
This is just a guess, not something I really know.
我认为您的问题与此问题有关:
I think your problem is related with this issue: