gpt4 book ai didi

c# - 休眠,无法解析属性

转载 作者:可可西里 更新时间:2023-11-01 08:07:05 26 4
gpt4 key购买 nike

我对 NHibenate 有疑问。当我运行 queryover 时,出现错误“无法解析属性:User.Name of:MegaOnlineChat.Core.Entities.Message”。我做错了什么?

实体对象

public class EntityObject<TId>
{
public virtual Int32 Id { get; set; }
}
public class User:EntityObject<Int32>
{
public virtual String Name { get; set; }
public virtual String Password { get; set; }
public virtual Boolean Admin { get; set; }
public virtual IList<Message> Messages { get; set; }
}
public class Message:EntityObject<Int32>
{
public virtual String Text { get; set; }
public virtual User User { get; set; }
public virtual DateTime Date{ get; set; }
}

映射

public class UserMapping:ClassMap<User>
{
public UserMapping()
{
Table("Users");
Id(m => m.Id).GeneratedBy.Native();
Map(m => m.Name).Unique().Not.Nullable();
Map(m => m.Admin).Not.Nullable();
Map(m => m.Password).Not.Nullable();

HasMany(m => m.Messages).KeyColumn("User_id");
}
}
public class MessageMapping:ClassMap<Message>
{
public MessageMapping()
{
Table("Messages");

Id(m => m.Id).GeneratedBy.Native();

Map(m => m.Text).Not.Nullable();
Map(m => m.Date).Not.Nullable();

References(m => m.User, "User_id").Not.Nullable();
}
}

查询结束

NHibernateSession.QueryOver<Message>().Where(x => x.User.Name == name).List<Message>().Last();

最佳答案

QueryOver 与 Linq 查询提供程序不同。这样做会更容易:

NHibernateSession.Query<Message>().Where(x => x.User.Name == name).ToList();

但是如果你想使用 QueryOver,你需要显式地加入你想导航的关系:

NHibernateSession.QueryOver<Message>()
.JoinQueryOver(x => x.User) // navigate to the user on the relationship
.Where(u => u.Name == name) // this restriction is on the User now
.List();

您也可以使用用户的别名来做到这一点

关于c# - 休眠,无法解析属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12930477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com