Koen
Koen

Reputation: 3686

Mapping duration with NHibernate

I have a database view (Sql Server 2005) that has a column Duration (EndDate - StartDate) and I want that to be mapped to a TimeSpan property in .NET.

So far my best guess is to convert it in the view to an int, like this:

DATEDIFF(ms, StartDate, EndDate) * 10000 AS Duration

... but that seems an ugly solution to me.

What is the best way to do that?

EDIT: I also tried using a formula:

<property name="Duration" formula="EndDate - StartDate" type="timespan" />

... but that also generates an error (Invalid cast from 'DateTime' to 'Int64') so there's not added value there.

Upvotes: 1

Views: 412

Answers (1)

Daniel Dolz
Daniel Dolz

Reputation: 2421

Not the exact answer to your question, but... I would perform the difference in my nhibernate class.

myclass.cs

public virtual DateTime dstart {get;set;}
public virtual DateTime dend {get;set;}

// This property not being part oh nhibernate mapping
public TimeSpan MyDifference
{
  get {return dend.Subtract(dstart);
}

This way, you do not overload the RDBMS performing innecesary substracts when performing selects, and you make the calculation only when needed. It it not a mapped property nor does need to be (read only anyway). MyDifference can be used and nowone can tell or cares how or who makes the calculation

Hope this helps

Upvotes: 1

Related Questions