[英]Unmapped property in a mapped class (Fluent NHibernate 1.1)
Fluent NHibernate 1.1 (with a repository implementation using LINQ to NHibernate) in an ASP.NET MVC 2 project using Ninject. 使用Ninject的ASP.NET MVC 2项目中的Fluent NHibernate 1.1(使用从LINQ到NHibernate的存储库实现)。
I have a mapped class ( Station
) with a collection of another mapped class ( Report
). 我有一个映射的类( Station
)和另一个映射的类( Report
)的集合。 I would also like Station
to have a convenience property ( MostRecentReport
) that returns the Report
for that Station
with the most recent timestamp. 我还希望Station
拥有一个便利属性( MostRecentReport
),该属性返回具有最新时间戳的该Station
的Report
。
Stations
--------
Id
Reports
---------
Id
StationId
Timestamp
public abstract class Entity
{
public virtual int Id { get; private set; }
}
public class Station : Entity
{
public virtual IList<Report> Reports { get; private set; }
public Station()
{
Reports = new List<Report>();
}
}
public class Report : Entity
{
public virtual Station Station { get; set; }
public virtual DateTime Timestamp { get; set; }
}
public StationMap : ClassMap<Station>
{
public StationMap()
{
Id(s => s.Id);
HasMany<Report>(s => s.Reports)
.Table("Reports")
.KeyColumn("StationId");
}
}
public ReportMap : ClassMap<Report>
{
public ReportMap()
{
Id(r => r.Id);
References<Station>(r => r.Station, "StationId");
Map(r => r.Timestamp);
}
}
I naively tried adding an unmapped property that returned Reports.OrderByDescending(r => r.Timestamp).FirstOrDefault()
but that causes "could not resolve property: MostRecentReport" NHibernate.QueryException
s (even though I am not using auto mapping). 我天真地尝试添加一个未映射的属性,该属性返回Reports.OrderByDescending(r => r.Timestamp).FirstOrDefault()
但这导致“无法解析属性:MostRecentReport” NHibernate.QueryException
s(即使我没有使用自动映射)。 I also tried implementing it as a method with identical return, but that causes "Index was out of range" exceptions. 我还尝试将其实现为具有相同返回值的方法,但这会导致“索引超出范围”异常。
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 Formula()
, perhaps? 还是有某种方法可以用Formula()
映射它?
Fluent NHibernate configuration (occurs in my NinjectModule
implementation's Load
override): 流利的NHibernate配置(发生在我的NinjectModule
实现的Load
覆盖中):
ISessionFactory sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(Settings.ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Domain.Station>())
.BuildSessionFactory();
I'm not sure that adding unmapped properties to your Entities is such a good idea. 我不确定将未映射的属性添加到您的实体是否是一个好主意。 It's a better separation of concerns to add convenience properties to a ViewModel and use a repository query to get the value. 将便利属性添加到ViewModel并使用存储库查询来获取值是更好的关注点分离。
There is no reason that you would have to map a property like this -- NHibernate should just ignore it if you are not automapping. 没有理由您必须映射这样的属性-如果不进行自动映射,NHibernate应该只忽略它。
I happen to agree with David's answer, that you might be breaking the abstraction of your domain model. 我恰好同意David的回答,即您可能会破坏域模型的抽象。
That said, I think the answer is that you have an NHibernate query (HQL/Criteria/LINQ) somewhere (a repository?) that is trying to access this property. 就是说,我认为答案是您正在某个NHibernate查询(HQL / Criteria / LINQ)某个地方(一个存储库?)试图访问此属性。 That is, it isn't in the mappings that is the problem, it is in trying to use MostRecentReport
when querying the database/cache. 也就是说,问题不在于映射, MostRecentReport
在查询数据库/缓存时尝试使用MostRecentReport
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.