Fluent 和 Nhibernate 中非法访问加载集合

发布于 2024-11-17 11:04:02 字数 2138 浏览 1 评论 0原文

我无法理解为什么会出现这个问题。如果有任何错误,请告诉我,我对这个主题很陌生。

public class Department
{
    public virtual int Dept_id { get; set; }
    public virtual String Dept_name { get; set; }
    public virtual IList<Student> Students { get; set; }
    //public virtual ICollection<Student> Students { get; set; }
    public Department()
    {
        Students = new List<Student>();
    }
}
public class Student
{
    public Student()
    { 

    }
    //private int _Dept_id;
    //public virtual Guid StudentId { get; set; }
    public virtual Guid StudentId { get; set; }
    /*public virtual int Dept_id
    {
        get { return this._Dept_id; }
        set { this._Dept_id = value; }
    }*/
    public virtual int Dept_id { get; set; }
    public virtual String Name { get; set; }
    public virtual int Age { get; set; }
    public virtual String Address { get; set; }

    public virtual Department Department { get; set; }
}
public class DepartmentMap : ClassMap<Department>
{
    public DepartmentMap()
    {
        Table("Department");
        Id(x => x.Dept_id).Column("Dept_id");
        Map(x => x.Dept_name).Column("Dept_name");

        HasMany(x => x.Students).KeyColumn("Student_id").Inverse()
            .Cascade.All();

    }
}
public class StudentMap :ClassMap<Student>
{
    public StudentMap() 
    {
        Table("Student");
        Id(x => x.StudentId).Column("Student_id").GeneratedBy.GuidComb();  

        Map(x => x.Name);
        Map(x => x.Age);
        Map(x => x.Address);         

        References(x => x.Department).Column("Dept_id")
            .Not.Nullable().Not.LazyLoad();
    }
}

现在,当我尝试这段代码时,

[WebMethod(EnableSession = true)]
    public List<Student> Students()
    {
        IList<Student> student = new List<Student>();
        ISession session = NHibernateHelper.OpenSession();
        student = session.Query<Student>().ToList();

        return student.ToList();
    }

在加载系内学生列表时出现错误

非法访问加载集合

此代码中缺少什么以及为什么会发生这种情况?

I'm not able to understand why the problem is occurring. Please let me know if there are any errors, I am very new to this topic.

public class Department
{
    public virtual int Dept_id { get; set; }
    public virtual String Dept_name { get; set; }
    public virtual IList<Student> Students { get; set; }
    //public virtual ICollection<Student> Students { get; set; }
    public Department()
    {
        Students = new List<Student>();
    }
}
public class Student
{
    public Student()
    { 

    }
    //private int _Dept_id;
    //public virtual Guid StudentId { get; set; }
    public virtual Guid StudentId { get; set; }
    /*public virtual int Dept_id
    {
        get { return this._Dept_id; }
        set { this._Dept_id = value; }
    }*/
    public virtual int Dept_id { get; set; }
    public virtual String Name { get; set; }
    public virtual int Age { get; set; }
    public virtual String Address { get; set; }

    public virtual Department Department { get; set; }
}
public class DepartmentMap : ClassMap<Department>
{
    public DepartmentMap()
    {
        Table("Department");
        Id(x => x.Dept_id).Column("Dept_id");
        Map(x => x.Dept_name).Column("Dept_name");

        HasMany(x => x.Students).KeyColumn("Student_id").Inverse()
            .Cascade.All();

    }
}
public class StudentMap :ClassMap<Student>
{
    public StudentMap() 
    {
        Table("Student");
        Id(x => x.StudentId).Column("Student_id").GeneratedBy.GuidComb();  

        Map(x => x.Name);
        Map(x => x.Age);
        Map(x => x.Address);         

        References(x => x.Department).Column("Dept_id")
            .Not.Nullable().Not.LazyLoad();
    }
}

Now when I am trying this code

[WebMethod(EnableSession = true)]
    public List<Student> Students()
    {
        IList<Student> student = new List<Student>();
        ISession session = NHibernateHelper.OpenSession();
        student = session.Query<Student>().ToList();

        return student.ToList();
    }

it gives error in loading the students list inside the department as

illegal access to loading collection

What is lacking in this code and why this is happening?

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

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

发布评论

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

评论(1

韵柒 2024-11-24 11:04:02

对不起我的不好!!我做了一些改变,使其发挥作用..虽然不确定下面提到的想法是否有明显的缺点,但

将学生班级更改为:

public class Student
{
    public Student()
    { 

    }

    public virtual Guid StudentId { get; set; }
    public virtual int Dept_id
    {
        get { return Department.Dept_id; }
        set { this.Dept_id = Department.Dept_id; }
    }

    public virtual String Name { get; set; }
    public virtual int Age { get; set; }
    public virtual String Address { get; set; }

    public virtual Department Department { get; set; }
}

以及供参考的学生映射

References(x => x.Department).Column("Dept_id").Cascade.All();

注意:不应有单个 Dept Id 映射

,并将 DepartmentMap 更改为:

public DepartmentMap()
    {
        Table("Department");
        Id(x => x.Dept_id).Column("Dept_id");
        Map(x => x.Dept_name).Column("Dept_name");


        HasMany(x => x.Students).KeyColumn("Dept_id").AsBag();

    }

sorry my bad !! there are cetain changes i made which made it working .. though not sure of apparent shortcomings of the mentioned idea below

changed student class as :

public class Student
{
    public Student()
    { 

    }

    public virtual Guid StudentId { get; set; }
    public virtual int Dept_id
    {
        get { return Department.Dept_id; }
        set { this.Dept_id = Department.Dept_id; }
    }

    public virtual String Name { get; set; }
    public virtual int Age { get; set; }
    public virtual String Address { get; set; }

    public virtual Department Department { get; set; }
}

and student mapping for the reference as

References(x => x.Department).Column("Dept_id").Cascade.All();

Note : there should be no single Dept Id mapping

and changed the DepartmentMap as :

public DepartmentMap()
    {
        Table("Department");
        Id(x => x.Dept_id).Column("Dept_id");
        Map(x => x.Dept_name).Column("Dept_name");


        HasMany(x => x.Students).KeyColumn("Dept_id").AsBag();

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