欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

NHibernate 基于 ClassMapping 的 ManyToMany(多对多配置)

程序员文章站 2022-04-23 15:44:44
...

NHibernate 基于 ClassMapping 的 ManyToMany(多对多配置)

实体类

public class Library
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Book> Books { get; set; }

    public Library()
    {
        Books = new List<Book>();
    }
}

public class Book
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Library> Libraries { get; set; }
    public virtual IList<BookLevel> BookLevel { get; set; }

    public Book()
    {
        Libraries = new List<Library>();
        BookLevel = new List<BookLevel>();
    }
}

public class BookLevel
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<Book> Book { get; set; }

    public BookLevel()
    {
        Book = new List<Book>();
    }
}

ClassMapping 映射类

public class LibraryMap : ClassMapping<Library>
{
    public LibraryMap()
    {
        Lazy(false);
        Table("Library");
        Id(l => l.Id,map => { map.Column("id"); map.Generator(Generators.Identity); });
        Property(x => x.Name, map => map.Column("LibraryName"));

        this.Bag(x => x.Books, map => {
            map.Table("Book_Library");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("LibraryId"));
        }, m => m.ManyToMany(x => x.Column("BookId")));
    }
}

public class BookMap : ClassMapping<Book>
{
    public BookMap()
    {
        Lazy(false);
        Table("Book");
        Id(l => l.Id, map => { map.Column("id"); map.Generator(Generators.Identity); });
        Property(x => x.Name, map => map.Column("BookName"));

        this.Bag(x => x.Libraries, map => {
            map.Table("Book_Library");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("BookId"));
        }, m => m.ManyToMany(x=>x.Column("LibraryId")));

        this.Bag(x => x.BookLevel, map => {
            map.Table("Book_Level");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("BookId"));
        }, m => m.ManyToMany(x => x.Column("LevelId")));
    }
}

public class BookLevelMap : ClassMapping<BookLevel>
{
    public BookLevelMap()
    {
        Lazy(false);
        Table("BookLevel");
        Id(l => l.Id, map => { map.Column("id"); map.Generator(Generators.Identity); });
        Property(x => x.Name, map => map.Column("LevelName"));

        this.Bag(x => x.Book, map => {
            map.Table("Book_Level");
            map.Cascade(Cascade.All);
            map.Inverse(false);
            map.Lazy(CollectionLazy.Lazy);
            map.Key(key => key.Column("LevelId"));
        }, m => m.ManyToMany(x => x.Column("BookId")));
    }
}