📜 ⬆️ ⬇️

Lessons in FluentNHibernate c ASP.NET MVC and SQL Server. Part 1

What will happen? Lessons on FluentNHibernate on the example of a book site, where we will be able to view information about a book, display a list of books, filter it by genre, search by title, and add, edit, delete books. All types of table links (one-to-one, one-to-many, many-to-many), creating, editing, deleting records, filtering, searching, displaying page by page will be considered. Programs used in the lessons: Visual studio 2013, SQL Server 2008 Manager Studio.

For a quick look at the simple CRUD operation with NHibernate and MVC, I recommend reading the following article in English: www.dotnetjalps.com/2014/07/fluent-nhibernate-asp-net-mvc-crud.html .

Also useful articles can be found here: github.com/jagregory/fluent-nhibernate/wiki/Getting-started (eng); and its translation: habrahabr.ru/post/125135 .
')
More links in Russian: slynetblog.blogspot.com/2009/10/nhibernate-1.html

Content
Part 1. Run the first application
Part 2. Creating classes, mappings and filling database

1. STARTING THE FIRST APPLICATION
1.1 Installing Fluent NHibernate
Open Visual Studio, open the File menu, go to the New and Project submenus. In the window that opens, select ASP.NET MVC 4 (If .Net Framework 4 Versions is selected). Enter the name "BibliotecaTutor" and select the type "Basic". (I’ll shortly write File-> New-> Project , and RClick the right button)



Next, run the Nuget Package ( Tools-> Library Package Manager-> Package Manager Console ). Write the following line: Install-Package FluentNHibernate . Press Enter, and wait for the installation to complete FluentNHibernat'a.



If you need to install a specific version of FluentNhibernate, then look for a line on the NuGet website www.nuget.org/packages/FluentNHibernate/2.0.1 in the Version table.

1.2 Configuring FluentNHibernate
  1. Open the SQL Server Manager database and create the Biblioteca database there.
  2. Open Visual Studio, in the “Models” folder, create the “Book.cs” class (Models-> Book.cs)
  3. Create the NHibernate folder in Models and add the NHibernateHelper.cs class there ( Models-> NHibernate-> NHibernateHelper.cs )

using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using NHibernate; using NHibernate.Tool.hbm2ddl; public class NHibernateHelper { public static ISession OpenSession() { ISessionFactory sessionFactory = Fluently.Configure() // .     MS Sql Server 2008 .Database(MsSqlConfiguration.MsSql2008.ConnectionString(@"Server=..\SQLENTERPRISE; initial catalog= Biblioteca; Integrated Security=SSPI;") .ShowSql() ) //.  AddFromAssemblyOf NHibernate         (assembly).    . .Mappings(m =>m.FluentMappings.AddFromAssemblyOf<Book>()) //SchemeUpdate  /      (2  ==true) .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)) .BuildSessionFactory(); return sessionFactory.OpenSession(); } } 

1.3 Links, Classes, Mappings
The following tables will be created in the project: Book, Author, Genre, Series and Mind.



1.3.1 Links
One-to-one - any instance of entity A corresponds to only one instance of entity B, and vice versa. Example, Table Book and Mind . If I wrote a review of the book "Metro 2033", then I can not apply the exact same review of the book "Metro 2034", and on the "Metro 2033" my review of the book "Metro 2034" won't work.

One-to-one relationships are rarely used, since all these data can be successfully put into one table. It is used in those cases, if for some reason you want to divide one table into two tables. For example, those fields that are often empty, I put in a separate table.

“One-to-many” / “many-to-one” - any instance of entity A corresponds to 0, 1 or more instances of entity B, but any instance of entity B corresponds to only one instance of entity A. Example Book and Series . In one series / cycle there can be many books, but the book itself can only be included in one series / cycle.

What is the difference between one-to-many and many-to-one connections? Same as between the words "student portfolio" and "student portfolio." That is, it is important who in the relationship of two objects is the main one - the student or the portfolio.

“Many-to-many” - to any instance of entity A corresponds to 0, 1 or several instances of entity B, and to any instance of entity B corresponds to 0, 1 or several instances of entity A. Example Book and Author , Book and Genre . Over one book, for example, an anthology, several authors work, but the author may have several books. These tables are related to each other using intermediate tables such as Book_Author and Book_Genre .

1.3.2 Book class and its Mapping
Let's create a simple application that will display data on a web page about a book stored in a SQL Server database. We fill the class Book and create a mapping class for it.

 using System; using FluentNHibernate.Mapping; namespace BibliotecaTutor.Models { 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; } } //  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); } } } 

What is Mapping for? With it, we link the data of the Book class with the Book table (which will be created a little later), change the column names, define the types of links, and so on.

After creating the Model Book, create a controller ( Controller-> RClick-> Add-> Controller ) and name it HomeController . We write code that displays all books from the Book table.

  public ActionResult Index() { using (ISession session = NHibernateHelper.OpenSession()) { book = session.Query<Book>().ToList() return View(book); } } 

Next, we will create a strongly typed view (View) in which we will display all the entries from the books. (RClick on the red inscription View (book) -> Add View ). We put checkmarks and select the following parameters, as in the figure below (so that in Model Class you can choose the Build-> Build Solution project from the Model Book list).



The automatically generated code is displayed, in which all fields of the Book class are displayed. After creating the view, run the project (F5). A blank page will appear.

But if you open SQL Server Manager Studio and look in the Biblioteca database, then the Book table will appear there. Fill it with data.



(You can change the columns, for example, Description, make nVarChar (255) in text or NVarChar (Max)).



After that, refresh the page that will look like the image below.

Source: https://habr.com/ru/post/264907/


All Articles