如何将属性映射到 Fluent NHibernate 中的第一个属性?
给定 User
和 FailedLogin
之间的父子关系,其中用户多次登录失败。我想将其映射到,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不到直接的解决方案,但会有一个内部列表:
i cant see a direct solution but there would be with an internal list:
我选择的解决方案(我对此并不完全满意)是:
此解决方案确保只有一个
FailedLogin
与User
一起加载。它还可以正确保存与用户关联的新的失败登录。最后,它确保该实现对 User 类的使用者隐藏。不幸的是,这仍然给User
类带来了持久性问题。The solution I settled on, which I'm not entirely happy with, is:
This solution ensures that only a single
FailedLogin
is loaded with theUser
. 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 theUser
class. Unfortunately, this still imposes persistence concerns on theUser
class.