Reputation: 3254
I created the console application using Nhibernate.
I created hibernate.cfg.xml file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"
assembly="NHibernateDemo"
namespace="NHibernateDemo">
<session-factory>
<property name="connection.connection_string_name">default</property>
<property name="connection.driver_class">Nhibernate.Driver.SqlClientDriver</property>
<property name="dialect">Nhibernate.Dialect.MsSql2008Dialect</property>
<mapping assembly ="NHibernateDemo" />
</session-factory>
</hibernate-configuration>
and it's my code
using System;
using System.Linq;
using System.Reflection;
using HibernatingRhinos.Profiler.Appender.NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Linq;
namespace NHibernateDemo
{
internal class Program
{
static void Main(string[] args)
{
NHibernateProfiler.Initialize();
var cfg = new Configuration();
cfg.Configure("hibernate.cfg.xml");
var sessionFactory = cfg.BuildSessionFactory();
int newId;
using (var session = sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
var newCustomer = CreateCustomer();
Console.WriteLine("Before saving:");
Console.WriteLine(newCustomer);
session.Save(newCustomer);
newId = newCustomer.Id;
tx.Commit();
}
using (var session = sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
var newCustomer = session.Load<Customer>(newId);
Console.WriteLine("\nAfter saving:");
Console.WriteLine(newCustomer);
session.Save(newCustomer);
tx.Commit();
}
Console.WriteLine("Enter any key to exit...");
Console.ReadKey();
}
private static Customer CreateCustomer()
{
return new Customer
{
FirstName = "Jonh",
LastName = "Doe",
Points = 100,
HasGoldStatus = true,
// MemberSince = new DateTime(2012, 1, 1),
CreditRating = CustomerCreditRating.Neutral,
AverageRating = 42.44454647,
Adress = new Location()
{
Street = "123 Somewhere Avenue",
City = "Nowhere",
Province = "Alberta",
Country = "Canada"
}
};
}
}
}
but when I tried to debug my application, I got HibernateConfigException The 'assembly' attribute is not declared
How to fix that?
UPD if I remove attributes assembly
and namespace
from hibernate.cfg.xml file I get other error
MappingException was unhandled
UPD2 My mapping file (Build Action = Embedded Resource)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateDemo"
namespace="NHibernateDemo">
<class name="Customer">
<id name="Id">
<generator class="native" />
</id>
<property name="FirstName"/>
<property name="LastName"/>
<property name="AverageRating"/>
<property name="Points"/>
<property name="HasGoldStatus"/>
<property name="MemberSince" type="UtcDateTime"/>
<property name="CreditRating" type="CustomerCreditRatingType"/>
<component name="Adress">
<property name="Street"/>
<property name="City"/>
<property name="Province"/>
<property name="Country"/>
</component>
</class>
</hibernate-mapping>
Upvotes: 1
Views: 1860
Reputation: 27944
You need to add the assambly in code:
var cfg = new Configuration();
cfg.AddAssembly("NHibernateDemo");
cfg.Configure("hibernate.cfg.xml");
Upvotes: 1