开发者

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

开发者 https://www.devze.com 2023-01-16 10:07 出处:网络
I have an entity called Incident and a DTO called IncidentDTO. For now, IncidentDTO simply looks like this:

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

精彩评论

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

关注公众号