一对多映射流畅 NHibernate

发布于 2024-10-19 22:08:48 字数 1480 浏览 0 评论 0原文

我正在尝试建立一对多关系。我有以下映射:

    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 技术交流群。

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

发布评论

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

评论(1

遮了一弯 2024-10-26 22:08:48

将列添加到 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

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