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

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")));
    }
}
posted @ 2018-07-15 18:25  ㄡ落叶风行  阅读(285)  评论(0编辑  收藏  举报