# How to add the Second Mapping xml for NHibertnateSession.cs?

**URL:** <https://discourse.hibernate.org/t/how-to-add-the-second-mapping-xml-for-nhibertnatesession-cs/2707>\
**Category:** Hibernate ORM\
**Created:** [May 12, 2019, 9:38pm UTC](https://discourse.hibernate.org/t/how-to-add-the-second-mapping-xml-for-nhibertnatesession-cs/2707 "2019-05-12T21:38:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hello](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/hello/32/591_2.png) [@hello](https://discourse.hibernate.org/u/hello)\
**Post date:** [May 12, 2019, 9:38pm UTC](https://discourse.hibernate.org/t/how-to-add-the-second-mapping-xml-for-nhibertnatesession-cs/2707/1 "2019-05-12T21:38:22Z")

</div>

Background:  
I have two tables that is “Product” and “Employee”.

I am enable to retrieve the data from the Employee. The next phase is to do the same approach but this time for the “Product”.

Problem:  
What code should be applied in the class NHibertnateSession.cs for the file “Product.hbm.xml”?

The code I have today is only fitable for a single .hbm.xml in the class NHibertnateSession.cs

Info:  
\*I have retrieve the fundamental instruction from // [https://www.dotnetjalps.com/2013/09/asp-net-mvc-nhibernate-crud-getting-started.html](https://www.dotnetjalps.com/2013/09/asp-net-mvc-nhibernate-crud-getting-started.html)  
\*You also need to take account to that I need to apply more tables (.hbm.xml) in the future.  
\*Iam using C# with Visual Studio 2017

Thank you!

hibernate.cfg.xml

```auto
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">
      NHibernate.Connection.DriverConnectionProvider
    </property>
    <property name="connection.driver_class">
      NHibernate.Driver.SqlClientDriver
    </property>
    <property name="connection.connection_string">
      Server=fffff-PC\MSSQL2017DEV;database=BookStoreDB;Integrated Security=SSPI;
    </property>
    <property name="dialect">
      NHibernate.Dialect.MsSql2012Dialect
    </property>
  </session-factory>
</hibernate-configuration>

```

NHibertnateSession.cs

```auto
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace Swinkaran.Nhbnt.Web.Models
{
    public class NHibertnateSession
    {
        public static ISession OpenSession()
        {

            var configuration = new Configuration();
            var configurationPath = HttpContext.Current.Server.MapPath(@"~\Models\hibernate.cfg.xml");
            configuration.Configure(configurationPath);
            var employeeConfigurationFile = HttpContext.Current.Server.MapPath(@"\Mappings\Employee.hbm.xml");

            configuration.AddFile(employeeConfigurationFile);
            ISessionFactory sessionFactory = configuration.BuildSessionFactory();
            return sessionFactory.OpenSession();

        }
    }
}

```

Employee.cs

```auto
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Swinkaran.Nhbnt.Web.Models
{
    public class Employee
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string Designation { get; set; }
    }
}

```

Employee.hbm.xml

```auto
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="Swinkaran.Nhbnt.Web" namespace="Swinkaran.Nhbnt.Web.Models">
  <class name="Employee" table="Employee" dynamic-update="true" >
    <cache usage="read-write"/>
    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>
    <property name="FirstName" />
    <property name="LastName" />
    <property name="Designation" />
  </class>
</hibernate-mapping>

```

HomeController

```auto
        using (NHibernate.ISession session = NHibertnateSession.OpenSession())
        {
            var employees = session.Query<Employee>().ToList();
        }

```

```auto
USE [BookStoreDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Product](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Description] [varchar](50) NULL,
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

```
