Fluent Nhibernate:映射问题

发布于 2024-10-21 04:15:26 字数 893 浏览 1 评论 0原文

我对 Fluent Nhibernate 非常陌生。我陷入了一种情况。 请查找以下详细信息。

我们的表结构就像

表学生 { Student_Id, Name}
表学校 { School_Id, 名称}
表 LinkTable { School_Id, Student_Id}

LinkTable 仅包含学生和学校的 id。 [复合键]

关系就像 1) 一名学生可以属于 0 或 1 所学校。 2) 一所学校可以容纳许多学生。

谁能告诉我如何为每个文件完成映射?

或者让 mw 知道以下映射文件中有什么问题

现在,它给我错误,在 SchoolStudent 上找不到学生属性。

  public Student()
{
    Id(x => x.Id);
    Map(x => x.Name);
    HasOne(x => x.SchoolStudent).PropertyRef(r => r.Student);
}

public School()
{
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.SchoolStudent).KeyColumn("School_Id").Inverse().Cascade.AllDeleteOrphan();
}

public SchoolStudent()
{
    CompositeId().KeyReference(x => x.School, "School_Id")
            .KeyReference(x => x.Student, "Student_Id");
}

谢谢, 马赫什

I am very much new to Fluent Nhibernate. I am stuck with the one situation.
Please find bellow details about it.

Our table structure is like as

Table Student { Student_Id, Name}
Table School { School_Id, Name}
Table LinkTable { School_Id, Student_Id}

LinkTable contains only id of the Student and School. [Composite Key]

Relation is like
1) One student can be part of 0 or 1 School.
2) One School can contains many students.

Can anyone please let me know how the mapping will be done for each file?

or let mw know what is wrong in following mapping files

Right now, it is giving me error that Student Property is not found on SchoolStudent.

  public Student()
{
    Id(x => x.Id);
    Map(x => x.Name);
    HasOne(x => x.SchoolStudent).PropertyRef(r => r.Student);
}

public School()
{
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.SchoolStudent).KeyColumn("School_Id").Inverse().Cascade.AllDeleteOrphan();
}

public SchoolStudent()
{
    CompositeId().KeyReference(x => x.School, "School_Id")
            .KeyReference(x => x.Student, "Student_Id");
}

Thanks,
Mahesh

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

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

发布评论

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

评论(2

挽你眉间 2024-10-28 04:15:26

我会将其重写为如下内容:

Student.cs

public class Student 
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<School> Schools { get; set; }

    public Student() 
    {
        Schools = new List<School>();
    }
}

School.cs

public class School 
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Student> Students { get; set; }

    public School() 
    {
        Students = new List<Student>();
    }
}

StudentMap.cs

public class StudentMap : ClassMap<Student>
{
    public Student()
    {
        Id(x => x.Id    , "Student_Id");
        Map(x => x.Name , "Name");

        HasManyToMany(x => x.Schools).Table("LinkTable")
                                     .ParentKeyColumn("Student_Id")
                                     .ChildKeyColumn("School_Id");
    }
}

SchoolMap.cs >

public class SchoolMap: ClassMap<School>
{
    public School()
    {
        Id(x => x.Id    , "School_Id");
        Map(x => x.Name , "Name");

        HasManyToMany(x => x.Students).Table("LinkTable")
                                      .ParentKeyColumn("School_Id")
                                      .ChildKeyColumn("Student_Id");
    }
}

I would re-write it to something like this:

Student.cs

public class Student 
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<School> Schools { get; set; }

    public Student() 
    {
        Schools = new List<School>();
    }
}

School.cs

public class School 
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Student> Students { get; set; }

    public School() 
    {
        Students = new List<Student>();
    }
}

StudentMap.cs

public class StudentMap : ClassMap<Student>
{
    public Student()
    {
        Id(x => x.Id    , "Student_Id");
        Map(x => x.Name , "Name");

        HasManyToMany(x => x.Schools).Table("LinkTable")
                                     .ParentKeyColumn("Student_Id")
                                     .ChildKeyColumn("School_Id");
    }
}

SchoolMap.cs

public class SchoolMap: ClassMap<School>
{
    public School()
    {
        Id(x => x.Id    , "School_Id");
        Map(x => x.Name , "Name");

        HasManyToMany(x => x.Students).Table("LinkTable")
                                      .ParentKeyColumn("School_Id")
                                      .ChildKeyColumn("Student_Id");
    }
}
一片旧的回忆 2024-10-28 04:15:26

如果一个学生只能与 0 或 1 所学校关联,那么您可以考虑删除 LinkTable 和 SchoolStudent 类,而只将 School_Id 添加到 Student 表,并在 Student 类中添加对 School 的引用。

那么你的映射就像:

public Student()
{
    Id(x => x.Id);
    Map(x => x.Name);
    References(x => x.School);
}

public School()
{
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Student).Inverse().Cascade.AllDeleteOrphan();
}

If a student can only be associated with 0 or 1 schools, then you might consider dropping the LinkTable and the SchoolStudent class and just add School_Id to the Student table and a reference to School in the Student class.

Then your mappings are like:

public Student()
{
    Id(x => x.Id);
    Map(x => x.Name);
    References(x => x.School);
}

public School()
{
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Student).Inverse().Cascade.AllDeleteOrphan();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文