一对多映射流畅 NHibernate
我正在尝试建立一对多关系。我有以下映射:
public class User
{
public User()
{
UserCourses = new List<UserCourse>();
}
public virtual int Id { get; private set; }
public virtual IList<UserCourse> UserCourses { get; private set;}
}
public sealed class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id, "Id");
HasMany(x => x.UserCourses).Inverse().Cascade.All().Table("UserCourse");
Table("[USER]");
}
}
public sealed class UserCourseMap : ClassMap<UserCourse>
{
public UserCourseMap()
{
Id(x => x.Id, "Id");
References(x => x.User, "UserID");
Map(x => x.Role, "Role");
}
}
如果我尝试创建 User 对象的实例,然后尝试查看课程,则会出现以下异常:
var user = (from u in userRepository.Linq() // Fetch a user
where u.Username == username
select u).Single();
var courses = user.UserCourses.Single(); // wont work
{“列名“User_id”无效。\r\n列名“User_id”无效。”} 无法初始化集合:[Fringedivision.Rapp.Domain.User.UserCourses#1][SQL: SELECT usercourse0_.User_id as User4_1_, usercourse0_.Id as Id1_, usercourse0_.Id as Id1_0_, usercourse0_.Role as Role1_0_, usercourse0_.UserID作为 UserID1_0_ 来自 [UserCourse] usercourse0_ WHERE usercourse0_.User_id=?]
我似乎不明白问题是什么,有什么建议吗?如果我创建 UserCourse 对象的实例,则引用映射似乎可以工作。
I am trying to get a One-To-Many relation working. I have the following mappings:
public class User
{
public User()
{
UserCourses = new List<UserCourse>();
}
public virtual int Id { get; private set; }
public virtual IList<UserCourse> UserCourses { get; private set;}
}
public sealed class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id, "Id");
HasMany(x => x.UserCourses).Inverse().Cascade.All().Table("UserCourse");
Table("[USER]");
}
}
public sealed class UserCourseMap : ClassMap<UserCourse>
{
public UserCourseMap()
{
Id(x => x.Id, "Id");
References(x => x.User, "UserID");
Map(x => x.Role, "Role");
}
}
I am getting the following exception if I try to make an instance of a User object and then tries to view the courses:
var user = (from u in userRepository.Linq() // Fetch a user
where u.Username == username
select u).Single();
var courses = user.UserCourses.Single(); // wont work
{"Invalid column name 'User_id'.\r\nInvalid column name 'User_id'."}
could not initialize a collection: [Fringedivision.Rapp.Domain.User.UserCourses#1][SQL: SELECT usercourse0_.User_id as User4_1_, usercourse0_.Id as Id1_, usercourse0_.Id as Id1_0_, usercourse0_.Role as Role1_0_, usercourse0_.UserID as UserID1_0_ FROM [UserCourse] usercourse0_ WHERE usercourse0_.User_id=?]
I can't seem to understand what the problem is, any suggestions? The Reference mappings seems to work if I make an instance of a UserCourse object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将列添加到 has many
HasMany(x => x.UserCourses).KeyColumn("UserId")
或您所在的 fnh 版本中的实际语法add the column to the has many
HasMany(x => x.UserCourses).KeyColumn("UserId")
or whatever the actualy syntax is in the version of fnh you are on