如何将属性映射到 Fluent NHibernate 中的第一个属性?

发布于 2024-09-28 09:26:23 字数 980 浏览 2 评论 0原文

给定 UserFailedLogin 之间的父子关系,其中用户多次登录失败。我想将其映射到,

class User
{
    public virtual FailedLogin LastFailedLogin { get; set; }
}

class FailedLogin
{
    public virtual User User { get; set; }
    public virtual DateTime AttemptOn { get; set; }
}

以便 LastFailedLogin 属性包含具有最近 AttemptOn 日期和时间的 FailedLogin 。如果设置了 LastFailedLogin,则应将 FailedLogin 保存到数据库(即 .Cascade.SaveUpdate()) 注意,我不这样做出于性能原因,想要映射该用户的所有 FailedLogins 的集合

我完全无法为此编写流畅的映射。如何使用 Fluent NHibernate 映射它?

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        // What goes here for LastFailedLogin?
    }
}

public class FailedLoginMap: ClassMap<FailedLogin>
{
    public FailedLoginMap()
    {
        References(x => x.User).Not.Update();
        Map(x => x.AttemptOn).Not.Update();
    }
}

Given a parent child relationship between User and FailedLogin where a user has many failed logins. I'd like to map this into

class User
{
    public virtual FailedLogin LastFailedLogin { get; set; }
}

class FailedLogin
{
    public virtual User User { get; set; }
    public virtual DateTime AttemptOn { get; set; }
}

So that the LastFailedLogin property contains the FailedLogin with the most recent AttemptOn date and time. If LastFailedLogin is set it should save that FailedLogin to the database (i.e. .Cascade.SaveUpdate()) Note, I do not want to map a collection of all FailedLogins for this user for performance reasons.

I have been totally unable to write a fluent mapping for this. How do I map this using Fluent NHibernate?

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        // What goes here for LastFailedLogin?
    }
}

public class FailedLoginMap: ClassMap<FailedLogin>
{
    public FailedLoginMap()
    {
        References(x => x.User).Not.Update();
        Map(x => x.AttemptOn).Not.Update();
    }
}

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

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

发布评论

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

评论(2

抽个烟儿 2024-10-05 09:26:23

我看不到直接的解决方案,但会有一个内部列表:

class User
{
    internal virtual IList<FailedLogin> LastFailedLogins { get; set; }
    public virtual FailedLogin LastFailedLogin { get { LastFailedLogins.FirstOrDefault(); } set { LastFailedLogins.Insert(0, value); } }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        HasMany(u => u.LastFailedLogins)
            .OrderBy("AttemptOn desc");
    }
}

i cant see a direct solution but there would be with an internal list:

class User
{
    internal virtual IList<FailedLogin> LastFailedLogins { get; set; }
    public virtual FailedLogin LastFailedLogin { get { LastFailedLogins.FirstOrDefault(); } set { LastFailedLogins.Insert(0, value); } }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        HasMany(u => u.LastFailedLogins)
            .OrderBy("AttemptOn desc");
    }
}
偏闹i 2024-10-05 09:26:23

我选择的解决方案(我对此并不完全满意)是:

public class User
{
    private readonly IList failedLogins;
    public virtual FailedLogin LastFailedLogin
    {
        get
        {
            return failedLogins.FirstOrDefault();
        }
        set
        {
            failedLogins.Clear();
            failedLogins.Add(value);
        }
    }
}

public class UserMap : ClassMap<User>
{
    HasMany(Reveal.Member<User, IEnumerable<FailedLogin>>("failedLogins"))
        .Where("FailedLoginID=(SELECT TOP 1 a.FailedLoginID FROM FailedLogin AS a " + 
                "WHERE a.UserID = UserID ORDER BY a.AttemptOn DESC)")
        .Cascade.SaveUpdate().Access.CamelCaseField().Inverse();
}

此解决方案确保只有一个 FailedLoginUser 一起加载。它还可以正确保存与用户关联的新的失败登录。最后,它确保该实现对 User 类的使用者隐藏。不幸的是,这仍然给 User 类带来了持久性问题。

The solution I settled on, which I'm not entirely happy with, is:

public class User
{
    private readonly IList failedLogins;
    public virtual FailedLogin LastFailedLogin
    {
        get
        {
            return failedLogins.FirstOrDefault();
        }
        set
        {
            failedLogins.Clear();
            failedLogins.Add(value);
        }
    }
}

public class UserMap : ClassMap<User>
{
    HasMany(Reveal.Member<User, IEnumerable<FailedLogin>>("failedLogins"))
        .Where("FailedLoginID=(SELECT TOP 1 a.FailedLoginID FROM FailedLogin AS a " + 
                "WHERE a.UserID = UserID ORDER BY a.AttemptOn DESC)")
        .Cascade.SaveUpdate().Access.CamelCaseField().Inverse();
}

This solution ensures that only a single FailedLogin is loaded with the User. It also handles correctly saving a new failed login that is associated to the user. Finally it ensures that the implementation is hidden from consumers of the User class. Unfortunately, this still imposes persistence concerns on the User class.

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