Basic NHibernate 5.2 configuration



Created: 11/10/2019 02:21:15 p.m.






Basic NHibernate 5.2 configuration


Environment
--------------
Visual Studio 2019
NHibernate 5.2
Sql Server 2014


1) Database: I used only one simple table

USE [ProductosDB]
GO

/****** Object:  Table [dbo].[items]    Script Date: 11/10/2019 14:18:10 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[items](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
 CONSTRAINT [PK_items] PRIMARY KEY CLUSTERED 
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO



2) items.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2"
   assembly = "ConsoleApp1" namespace = "ConsoleApp1">

  <class name = "items">
    <id name = "id">
      <generator class = "identity"/>
    </id>

    <property name = "name"/>
  </class>

</hibernate-mapping>

3) App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section
      name="hibernate-configuration"
      type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
      />
  </configSections>
  <!--NHibernate Configuration (Adding Properties)-->

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
      <session-factory>
        <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
        <property name="connection.connection_string">
          Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ProductosDB;Data Source=XXXXX
        </property>

        <mapping assembly="ConsoleApp1" />
    </session-factory>
  </hibernate-configuration>
  
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />

    </startup>

  </configuration>

4) Console App

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate;

namespace ConsoleApp1
{
    public class items
    {
        public items()
        {

        }

        public virtual int id { get; set; }
        public virtual string name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            
            NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
            cfg.AddAssembly("ConsoleApp1");

            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();

            items i = new items()
            {
                name = "asdsaas"
            };

            
            session.Save(i);

            // commit all of the changes to the DB and close the ISession
            transaction.Commit();

            // Closing the session
            session.Close();

            Console.WriteLine("End");

            Console.ReadKey();
        }
    }
}


Thats all i have, and it works!



Comments:


Go to Comments