开发者

How do I stop AutoMapper from making two identical SELECT statements with NHibernate?

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 e开发者_如何学Pythonxactly identical. Why does AutoMapper generate two identical SELECT statements, and how do I prevent it from doing this?


A guess: Enumerating IQueryable creates a separate select for every item. Solve it with enumerating IList.

var incidents = session.Linq<Incident>().ToList();

I would probably do this to prevent another problem.

int someReasonableNumber = 1000;
var incidents = session.Linq<Incident>().Take(someReasonableNumber).ToList();

This is just a guess, not something I really know.


I think your problem is related with this issue:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜