Note that there are some explanatory texts on larger screens.

plurals
  1. POUnmapped property in a mapped class (Fluent NHibernate 1.1)
    primarykey
    data
    text
    <h2>Background</h2> <p>Fluent NHibernate 1.1 (with a repository implementation using LINQ to NHibernate) in an ASP.NET MVC 2 project using Ninject.</p> <p>I have a mapped class (<code>Station</code>) with a collection of another mapped class (<code>Report</code>). I would also like <code>Station</code> to have a convenience property (<code>MostRecentReport</code>) that returns the <code>Report</code> for that <code>Station</code> with the most recent timestamp.</p> <h2>Partial Schema</h2> <pre><code>Stations -------- Id Reports --------- Id StationId Timestamp </code></pre> <h2>Partial Code</h2> <pre><code>public abstract class Entity { public virtual int Id { get; private set; } } public class Station : Entity { public virtual IList&lt;Report&gt; Reports { get; private set; } public Station() { Reports = new List&lt;Report&gt;(); } } public class Report : Entity { public virtual Station Station { get; set; } public virtual DateTime Timestamp { get; set; } } public StationMap : ClassMap&lt;Station&gt; { public StationMap() { Id(s =&gt; s.Id); HasMany&lt;Report&gt;(s =&gt; s.Reports) .Table("Reports") .KeyColumn("StationId"); } } public ReportMap : ClassMap&lt;Report&gt; { public ReportMap() { Id(r =&gt; r.Id); References&lt;Station&gt;(r =&gt; r.Station, "StationId"); Map(r =&gt; r.Timestamp); } } </code></pre> <p>I naively tried adding an unmapped property that returned <code>Reports.OrderByDescending(r =&gt; r.Timestamp).FirstOrDefault()</code> but that causes "could not resolve property: MostRecentReport" <code>NHibernate.QueryException</code>s (even though I am not using auto mapping). I also tried implementing it as a method with identical return, but that causes "Index was out of range" exceptions.</p> <p>Is there a way to make it work with either the property (preferable) or method approach? Or is there some way to map it with <code>Formula()</code>, perhaps?</p> <h2>Update</h2> <p>Fluent NHibernate configuration (occurs in my <code>NinjectModule</code> implementation's <code>Load</code> override):</p> <pre><code>ISessionFactory sessionFactory = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008.ConnectionString(Settings.ConnectionString)) .Mappings(m =&gt; m.FluentMappings.AddFromAssemblyOf&lt;Domain.Station&gt;()) .BuildSessionFactory(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload