映射类中未映射的属性 (Fluent NHibernate 1.1)

发布于 2024-09-11 20:52:45 字数 1964 浏览 6 评论 0原文

背景

使用 Ninject 在 ASP.NET MVC 2 项目中实现流畅的 NHibernate 1.1(具有使用 LINQ to NHibernate 的存储库实现)。

我有一个映射类 (Station) 和另一个映射类 (Report) 的集合。我还希望 Station 有一个方便的属性 (MostRecentReport),它返回该 StationReport ,其中包含最近的时间戳。

部分架构

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);
    }
}

我天真地尝试添加一个未映射的属性,该属性返回 Reports.OrderByDescending(r => r.Timestamp).FirstOrDefault() 但这会导致“无法解析属性:MostRecentReport”NHibernate.QueryException(即使我没有使用自动映射)。我还尝试将其实现为具有相同返回值的方法,但这会导致“索引超出范围”异常。

有没有办法让它与属性(首选)或方法方法一起工作?或者有什么方法可以用 Formula() 来映射它?

更新

Fluent NHibernate 配置(发生在我的 NinjectModule 实现的 Load 覆盖中):

ISessionFactory sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(Settings.ConnectionString))
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Domain.Station>())
    .BuildSessionFactory();

Background

Fluent NHibernate 1.1 (with a repository implementation using LINQ to NHibernate) in an ASP.NET MVC 2 project using Ninject.

I have a mapped class (Station) with a collection of another mapped class (Report). I would also like Station to have a convenience property (MostRecentReport) that returns the Report for that Station with the most recent timestamp.

Partial Schema

Stations
--------
Id

Reports
---------
Id
StationId
Timestamp

Partial Code

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.QueryExceptions (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.

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?

Update

Fluent NHibernate configuration (occurs in my NinjectModule implementation's Load override):

ISessionFactory sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(Settings.ConnectionString))
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Domain.Station>())
    .BuildSessionFactory();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-09-18 20:52:45

您没有理由必须映射这样的属性——如果您不自动映射,NHibernate 应该忽略它。

我碰巧同意大卫的回答,你可能会破坏领域模型的抽象。

也就是说,我认为答案是您在某处(存储库?)有一个 NHibernate 查询(HQL/Criteria/LINQ)正在尝试访问此属性。也就是说,问题不在映射中,而是在查询数据库/缓存时尝试使用 MostRecentReport

There is no reason that you would have to map a property like this -- NHibernate should just ignore it if you are not automapping.

I happen to agree with David's answer, that you might be breaking the abstraction of your domain model.

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. That is, it isn't in the mappings that is the problem, it is in trying to use MostRecentReport when querying the database/cache.

南七夏 2024-09-18 20:52:45

我不确定将未映射的属性添加到您的实体是个好主意。将便利属性添加到 ViewModel 并使用存储库查询来获取值是一种更好的关注点分离。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文