开发者

Fluent NHibernate: Which is better... selecting from a view or performing joins to retrieve MIN and MAX values from related table?

Disclaimer: I'm pretty new to NHibernate/Fluent NHibernate.

I have a table of events:

ID UniqueIdentifier,
Name varchar(100),
Details varchar(MAX)
....

I also have a lookup table that shows locations of the events:

ID UniqueIdentifier,
StartDate datetime,
EndDate datetime,
City varchar(100)
....

What I want to do is to return the MIN(StartDate) and MAX(EndDate) values from my locations table when returning a list of my events.

Now, traditionally, I would construct a View in SQL for my event table that returned these aggregate values. From my understanding, if I were to do this when using NHibernate, I would need to create two Fluent maps, one for viewing (that references my SQL View), and one for inserting/editing, that references my table.

Am I correct?

The other option I was thinking is that within my Fluent mapping, I could someh开发者_高级运维ow perform the aggregate MIN and MAX functions against the related locations table.

Which is the preferred method, and how would I go about accomplishing the task?

Thanks - Joel


if the min and max is for specific events you can map it like:

Map(x => x.MaxDate).Formula("(SELECT MAX(EndDate) FROM locations l WHERE l.ID = ID)");
Map(x => x.MinDate).Formula("(SELECT MIN(StartDate) FROM locations l WHERE l.ID = ID)");

Note: these Properties are readonly (never written back)

if it should be global use futures (3 queries issued as one roundtrip)

var mindate = session.QueryOver().FutureValue();
var maxdate = session.QueryOver().FutureValue();
var events = session.QueryOver().Future();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜