public class Book { // public virtual int Id { get; set; } // public virtual string Name { get; set; } // public virtual string Description { get; set; } // public virtual int MfRaiting { get; set; } // public virtual int PageNumber { get; set; } // public virtual string Image { get; set; } // ( !) public virtual DateTime IncomeDate { get; set; } // (--) // ISet IList? (IList) JOIN , JOIN, ISet public virtual ISet<Genre> Genres { get; set; } // (--) public virtual Series Series { get; set; } // (--) private Mind _mind; public virtual Mind Mind { get { return _mind ?? (_mind = new Mind()); } set { _mind = value; } } // (--) public virtual ISet<Author> Authors { get; set; } // , null . public Book() { // ( -- , , ) Genres = new HashSet<Genre>(); Authors = new HashSet<Author>(); } } // Book public class BookMap : ClassMap<Book> { public BookMap() { Id(x => x.Id); Map(x => x.Name); Map(x => x.Description); Map(x => x.MfRaiting); Map(x => x.PageNumber); Map(x => x.Image); Map(x => x.IncomeDate); // -- HasManyToMany(x => x.Genres) // All - , , //// .Cascade.SaveUpdate() // Genre! .Table("Book_Genre"); HasManyToMany(x => x.Authors) .Cascade.SaveUpdate() .Table("Book_Author"); // References(x => x.Series); // --. . HasOne(x => x.Mind).Cascade.All().Constrained(); } }
public class Author { public virtual int Id { get; set; } //- public virtual string Name { get; set; } // public virtual string Biography { get; set; } // public virtual ISet<Book> Books { get; set; } // public Author() { Books=new HashSet<Book>(); } } // public class AuthorMap : ClassMap<Author> { public AuthorMap() { Id(x => x.Id); Map(x => x.Name); Map(x => x.Biography); // -- HasManyToMany(x => x.Books) // All - , , // .Cascade.All() // . (Book) . .Inverse() // Book! .Table("Book_Author"); } }
public class Genre { public virtual int Id { get; set; } // public virtual string Name { get; set; } // public virtual string EngName { get; set; } // public virtual ISet<Book> Books { get; set; } // public Genre() { Books=new HashSet<Book>(); } } // public class GenreMap : ClassMap<Genre> { public GenreMap() { Id(x => x.Id); Map(x => x.Name); Map(x => x.EngName); // -- HasManyToMany(x => x.Books) // All - , , // .Cascade.All() // . (Book) . .Inverse() // Book! .Table("Book_Genre"); } }
public class Mind { public virtual int Id { get; set; } // public virtual string MyMind { get; set; } // public virtual string MindFantLab { get; set; } // public virtual Book Book { get; set; } } // ind public class MindMap:ClassMap<Mind> { public MindMap() { Id(x => x.Id); Map(x => x.MyMind); Map(x => x.MindFantLab); // HasOne(x => x.Book); } }
public class Series { public virtual int Id { get; set; } public virtual string Name { get; set; } // IList, ISet, Book, Series , ISet public virtual IList<Book> Books { get; set; } // . public Series() { Books = new List<Book>(); } } public class SeriesMap : ClassMap<Series> { public SeriesMap() { Id(x => x.Id); Map(x => x.Name); // -- HasMany(x => x.Books) //// . (Book) . .Inverse() } }
Book | Author |
---|---|
HasManyToMany (x => x.Genres) .Cascade.SaveUpdate () .Table ("Book_Author"); | HasManyToMany (x => x.Books) .Cascade.All () .Inverse () .Table ("Book_Author"); |
Book | Series |
---|---|
References (x => x.Series) .Cascade.SaveUpdate (); | HasMany (x => x.Books) .Inverse (); |
Book | Mind |
---|---|
HasOne (x => x.Mind) .Cascade.All (). Constrained (); | HasOne (x => x.Book); |
public ActionResult Index() { using (ISession session = NHibernateHelper.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { //, var createBook = new Book(); createBook.Name = "Metro2033"; createBook.Description = " "; createBook.Authors.Add(new Author { Name = "" }); createBook.Genres.Add(new Genre { Name = " " }); createBook.Series = new Series { Name = "" }; createBook.Mind = new Mind { MyMind = " " }; session.SaveOrUpdate(createBook); // ( ) //var series = session.Get<Series>(1); //var updateBook = session.Get<Book>(1); //updateBook.Name = "Metro2034"; //updateBook.Description = ""; //updateBook.Authors.ElementAt(0).Name = ""; //updateBook.Genres.ElementAt(0).Name = ""; //updateBook.Series = series; //updateBook.Mind.MyMind = "11111"; //session.SaveOrUpdate(updateBook); // ( ) //var deleteBook = session.Get<Book>(1); //session.Delete(deleteBook); transaction.Commit(); } Genre genreAl = null; Author authorAl = null; Series seriesAl = null; Mind mindAl = null; var books = session.QueryOver<Book>() //Left Join Genres .JoinAlias(p => p.Genres, () => genreAl, JoinType.LeftOuterJoin) .JoinAlias(p => p.Authors, () => authorAl, JoinType.LeftOuterJoin) .JoinAlias(p => p.Series, () => seriesAl, JoinType.LeftOuterJoin) .JoinAlias(p => p.Mind, () => mindAl, JoinType.LeftOuterJoin) // id Book. .TransformUsing(Transformers.DistinctRootEntity).List(); return View(books); } }
@model IEnumerable<NhibernateMVC.Models.Book> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <style> th, td { border: 1px solid; } </style> </head> <body> <p>@Html.ActionLink("Create New", "Create")</p> <table> <tr> <th>@Html.DisplayNameFor(model => model.Name)</th> <th>@Html.DisplayNameFor(model => model.Mind)</th> <th>@Html.DisplayNameFor(model => model.Series)</th> <th>@Html.DisplayNameFor(model => model.Authors)</th> <th>@Html.DisplayNameFor(model => model.Genres)</th> <th></th> </tr> @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem => item.Name)</td> <td>@Html.DisplayFor(modelItem => item.Mind.MyMind)</td> @{string strSeries = item.Series != null ? item.Series.Name : null;} <td>@Html.DisplayFor(modelItem => strSeries)</td> <td> @foreach (var author in item.Authors) { string strAuthor = author != null ? author.Name : null; @Html.DisplayFor(modelItem => strAuthor) <br /> } </td> <td> @foreach (var genre in item.Genres) { string strGenre = genre!= null ? genre.Name : null; @Html.DisplayFor(modelItem => strGenre) <br /> } </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | @Html.ActionLink("Details", "Details", new { id = item.Id }) | @Html.ActionLink("Delete", "Delete", new { id = item.Id }) </td> </tr> } </table> </body> </html>
// public class TwoDShape { // public virtual int Width { get; set; } // public virtual int Height { get; set; } } // public class Triangle : TwoDShape { // public virtual int Id { get; set; } // public virtual string Style { get; set; } }
// public class TriangleMap : ClassMap<Triangle> { public TriangleMap() { Id(x => x.Id); Map(x => x.Style); Map(x => x.Height); Map(x => x.Width); } }
Source: https://habr.com/ru/post/264961/
All Articles