Reputation: 121
I am currently attempting to connect to a MySQL database using NHibernate. The basic action of this so far is to Add a new User to the database within a UI. Currently my code looks like this:-
hibernate.cfg.xml
<?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.MySqlDataDriver
</property>
<property name="connection.connection_string">
Server=*********;Database=********;Username=*********;Password=*********;
</property>
<property name="dialect">
NHibernate.Dialect.MySQLDialect
</property>
<property name="show_sql">
true
</property>
</session-factory>
</hibernate-configuration>
User.cs
namespace APIDB.Domain
{
class User
{
public virtual Guid Id { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual bool LoggedIn { get; set; }
}
}
User.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="WindowsFormsApplication1"
namespace="WindowsFormsApplication1.Domain">
<class name="User">
<id name="Id">
<column name="Id" />
<generator class="guid" />
</id>
<property name="Username" column="Username" />
<property name="Password" column="Password"/>
<property name="LoggedIn" column="LoggedIn"/>
</class>
</hibernate-mapping>
NHibernateHelper.cs
namespace APIDB
{
class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if(_sessionFactory == null)
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(User).Assembly);
_sessionFactory = cfg.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
DAO Add Method
namespace APIDB
{
class DAO
{
public void Add(User newUser)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using(ITransaction transaction = session.BeginTransaction())
{
session.Save(newUser);
transaction.Commit();
}
}
}
}
}
Call in main UI
public void addClient(String username, String password)
{
LoadHibernateCfg();
DAO dao = new DAO();
var tempUser = new User { Username = username, Password = password, LoggedIn = true };
dao.Add(tempUser);
}
public static void LoadHibernateCfg()
{
var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(User).Assembly); //ERROR OCCURS ON THIS LINE
new SchemaExport(cfg).Execute(true, true, false);
}
When I try to run this code, a MappingException is thrown and I get the message :-
NHibernate Could not compile the mapping document: WindowsFormsApplication1.Mapping.User.hbm.xml
Any help to rectify this issue is greatly appreciated.
Upvotes: 1
Views: 165
Reputation: 937
It looks like the namespace in the mapping "WindowsFormsApplication1.Domain" differs from the actual domain namespace "APIDB.Domain". The assembly name could also be wrong but I don't know what the generated assembly name is for your project.
Upvotes: 2