Reputation: 57
I'm trying to map the following object:
public class DatabaseInfo
{
public virtual DateTime CurrentDateTime { get; set; }
}
with the following map:
public class DatabaseInfoMap : ClassMapping<DatabaseInfo>
{
public DatabaseInfoMap()
{
Property(x => x.CurrentDateTime, map => { map.Formula("(SELECT CURRENT UTC TIMESTAMP AS CurrentDateTime)"); });
}
}
I am getting a validation error, as the Map does not contain a 'Table' definition. Is it not possible to map obejcts without having to make a Table or View?
I'm not sure if this is the right approach for me to get the database time via NHibernate. Any help is appreciated.
Upvotes: 0
Views: 77
Reputation: 30813
Since there is no table mapping as an entity makes no sense (no identity). I suggest:
public class DatabaseInfo
{
public static DatabaseInfo Get(ISession session)
{
return session.CreateSQLQuery("SELECT CURRENT UTC TIMESTAMP AS CurrentDateTime").SetResultTransformer(Transformers.AliasToBean<DatabaseInfo>()).UniqueResult<DatabaseInfo>();
}
public DateTime CurrentDateTime { get; protected set; }
}
Upvotes: 1
Reputation: 6821
Can't you just use:
public class DatabaseInfo
{
public DateTime CurrentDateTime { get; set; } = DateTime.UtcNow;
}
When NH hydrates your object, or a graph containing this object, this property will be set with that default value.
Upvotes: 0