Zhenyu’s Blog

陌上发花,可以缓缓醉矣, 忍把浮名,换了浅酌低唱。

nHibernate Mapping by Code - Many to Many

| Comments

Mapping by code是nHibernate3.2新增的功能,网络上及官方doc相关的介绍都很少。下面是如何使用mapping by code的方式 配置多对多关联的例子。

关于如何配置nhibernate使用mapping by code,参考nHibernate Mapping By Code - Introduction

实体类

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

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

数据库表(基于MS SQL Server 2012)

CREATE TABLE [dbo].[Student](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,    
 CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED ([Id] ASC)

CREATE TABLE [dbo].[Course](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,    
 CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ([Id] ASC)

CREATE TABLE [dbo].[Student_Course](
    [StudentId] [int] NOT NULL,
    [CourseId] [int] NOT NULL,    
 CONSTRAINT [PK_Student_Course] PRIMARY KEY CLUSTERED ([StudentId] ASC, [CourseId] ASC)

ALTER TABLE [dbo].[Student_Course] ADD CONSTRAINT [FK_Student_Course_StudentId] FOREIGN KEY([StudentId])
REFERENCES [dbo].[Student] ([Id])

ALTER TABLE [dbo].[Student_Course] ADD CONSTRAINT [FK_Student_Course_CourseId] FOREIGN KEY([CourseId])
REFERENCES [dbo].[Course] ([Id])

 

映射类

public class CourseMapping : ClassMapping<Course>
{
    public CourseMapping()
    {
        Table("Course");
        Id(course => course.Id, map => map.Generator(Generators.Identity));
        Property(course => course.Name);
        Bag(course => course.Students, map =>
        {
            map.Table("Student_Course");
            map.Key(keyMapper => keyMapper.Column("CourseId"));
        }, rel => rel.ManyToMany(m => m.Column("StudentId")));
    }
}

public class StudentMapping : ClassMapping<Student>
{
    public StudentMapping()
    {
        Table("Student");
        Id(student => student.Id, map => map.Generator(Generators.Identity));
        Property(student => student.Name);
        Bag(student => student.Courses, map =>
            {
                map.Table("Student_Course");
                map.Key(keyMapper => keyMapper.Column("StudentId"));
            }, rel => rel.ManyToMany(m => m.Column("CourseId")));
    }
}

Comments