NHibernate 映射属性超越 LINQ 查询

发布于 2024-09-19 17:30:37 字数 1031 浏览 3 评论 0原文

我的应用程序具有以下实体:

public class User
{
    public virtual int UserID { get; set; }
    public virtual Membership LatestMembership { get { return Membership.First(); } }
    public virtual IList<Membership> Membership { get; set; }

    public User()
    {
        Membership = new List<Membership>();
    }
}

使用以下映射:

public UserMap()
{
    Table("Users");
    Id(x => x.UserID);
    HasMany(x => x.Membership)
        .KeyColumn("UserID")
        .OrderBy("DateAdded DESC")
        .Inverse()
        .Cascade.All();
}

针对用户的LatestMembership 属性只需从Membership 集合中获取第一条记录(按顺序排列,以便较新的记录位于顶部)。

到目前为止一切顺利,但是现在说我想要执行以下操作(我知道这将返回所有内容,但我只是使用它作为示例):

var users = session.Linq<User>()
    .Where(u => u.LatestMembership.DateAdded < DateTime.UtcNow);

抛出错误,因为 LatestMembership 属性超出了 nhibernate linq 提供程序的功能。到目前为止,我唯一的解决方案是将其转换为列表,然后应用 where 条件,但我想这对于大型数据库来说可能会变得相当不够。

我想知道是否有其他方法可以绘制此图或您的建议是什么。谢谢

my application has the following entity:

public class User
{
    public virtual int UserID { get; set; }
    public virtual Membership LatestMembership { get { return Membership.First(); } }
    public virtual IList<Membership> Membership { get; set; }

    public User()
    {
        Membership = new List<Membership>();
    }
}

With the following mapping:

public UserMap()
{
    Table("Users");
    Id(x => x.UserID);
    HasMany(x => x.Membership)
        .KeyColumn("UserID")
        .OrderBy("DateAdded DESC")
        .Inverse()
        .Cascade.All();
}

The LatestMembership property against the user simply grabs the first record from the Membership collection (which is ordered so that the newer records are at the top).

So far so good, however now say i want to do the following (i know this will return them all but i'm just using this as an example):

var users = session.Linq<User>()
    .Where(u => u.LatestMembership.DateAdded < DateTime.UtcNow);

An error is thrown because the LatestMembership property is beyond the nhibernate linq providers capabilities. The only solution i have so far is to convert it to a list and then apply the where condition but i'd imagine this could become pretty insuficient for a large database.

I was wondering if there was an alternative way i could map this or what your recommendations are. Thanks

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

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

发布评论

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

评论(1

煮茶煮酒煮时光 2024-09-26 17:30:37

不幸的是,使用当前的 Linq 提供程序实际上不可能做到这一点。您可以做的一件事是将要查询的LatestMembership 属性映射为只读,并使用映射中的公式来导出它的值。您可以在我对 这篇文章

另一种可能的解决方案是从Membership 方面进行攻击;查询与相关用户匹配的最新会员资格并进行相应的过滤。

Unfortunately, this isn't really possible using the current Linq provider. One thing you can do is to map the properties of the LatestMembership you want to query on as read-only and use formulas in your mappings to derive the values for it. You can see some further details in my answer to this post.

The other possible solution is to attack this from the Membership side of things; query for the latest membership matching the user in question and filter accordingly.

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