Hi, I´m new with NHIbernate....if anyone know the answer...
I open a webApplication an configured the files like this :
web.config
<!-- Add this element -->
<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Server=(local);initial catalog=valmir;Integrated Security=SSPI</property>
</session-factory>
</hibernate-configuration>
cliente.cs
public class Cliente
{
public Cliente()
{
}
private int idCliente;
public int getIdCliente()
{
return idCliente;
}
public void setICliente(int value)
{
idCliente = value;
}
private string descricaoCliente;
public string getDescricaoCliente()
{
return descricaoCliente;
}
public void setDescricaoCliente(string value)
{
descricaoCliente = value;
}
}
cliente.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Cliente" table="Cliente">
<id name="idCliente" column="idCliente" type="Int32" length="4" unsaved-value="0">
</id>
<property name="descricaoCliente" column= "descricaoCliente" type="String" length="50"/>
</class>
</hibernate-mapping>
AND in the webform
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NHibernate;
using NHibernate.Cfg;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//ISessionFactory s = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
ISession session = NHibernateHelper.GetCurrentSession();
ITransaction tx = session.BeginTransaction();
Cliente c = new Cliente();
c.setICliente(9999);
c.setDescricaoCliente("9999");
session.Save(c);
tx.Commit();
}
}
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
sessionFactory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
AND When I run GET THE MESSAGE Unknown entity class !!!
Can someone help me !!! Thanks.
|