Hey everyone I am new to ORM, Im using Fluent NHibernate in my CRUD seems to have a problem in my search, it will return an empty or null value and also I can insert data, but when i insert another record its seems updating bcoz it replace my previous record. I try doing some google but still no used. Can anyone give me some tutorial links.
My Code:
employee Class in objclass folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent.objclass
{
public class employees
{
public virtual int employee_id { get; set; }
public virtual string employee_code { get; set; }
public virtual string last_name { get; set; }
public virtual string first_name { get; set; }
public virtual string middle_initial { get; set; }
ect..
}
}
My Mapping Class in map class folder
using fluent.objclass;
using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent.mapclass
{
public class employeesMap: ClassMap<employees>
{
public employeesMap()
{
Id(x => x.employee_id);
Map(x => x.employee_code);
Map(x => x.last_name);
Map(x => x.first_name);
Map(x => x.middle_initial);
ect..
}
}
}
My Repository in repository folder
using fluent.objclass;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent.repository
{
public class emp_repository
{
public void INSERT(employees newEmp)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(newEmp);
transaction.Commit();
}
}
}
public employees GetemployeesbyLName(int input)
{
using (ISession session = NHibernateHelper.OpenSession())
{
var result = session.QueryOver<employees>().Where(x => x.employee_id == input).SingleOrDefault();
return result ?? new employees();
}
}
}
}
My NHibernateHelper
using fluent.objclass;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fluent
{
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
InitializeSessionFactory();
return _sessionFactory;
}
}
private static void InitializeSessionFactory()
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(@"Server=ARK\DARKAGE;Database=PNH;Trusted_Connection=True;")
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<employees>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(true, true))
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
my code snipe
using FluentNHibernate.Mapping;
using fluent.objclass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NHibernate.Linq;
using fluent.repository;
namespace fluent
{
public partial class fluent : Form
{
emp_repository repo = new emp_repository();
public fluent()
{
InitializeComponent();
}
private void bntADD_Click(object sender, EventArgs e)
{
var emp = new employees
{
employee_code = txtBAR.Text.Trim(' '),
last_name = txtLNM.Text.Trim(' '),
first_name = txtFNM.Text.Trim(' '),
middle_initial = txtMNM.Text.Trim(' '),
ect...
};
repo.INSERT(emp);
MessageBox.Show(txtLNM.Text.Trim(' ') + "Successfully Added To Record");
}
private void bntSE_Click(object sender, EventArgs e)
{
employees emp = repo.GetemployeesbyLName(1);
MessageBox.Show(emp.last_name);
}
}
}
Finally my Table
USE [PNH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[employees](
[employee_id] [int] IDENTITY(1,1) NOT NULL,
[employee_code] [nvarchar](255) NULL,
[last_name] [nvarchar](255) NULL,
[first_name] [nvarchar](255) NULL,
[middle_initial] [nvarchar](255) NULL,
ect...
PRIMARY KEY CLUSTERED
(
[employee_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
:( sorry for my poor english and alignment :( Thanks in advance