Hi,
I have 3 projects (meaning 3 different assembly) like below
Symbonix.Web ---> The main asp.net website
Symbonix.Library ---> My code library
Symbonix.BLL -----> My project business logic classes
I put a configuration file called "nhibernate.cfg.xml" in the web root meaning ~/nhibernate.cfg.xml ( i customized the name from hibernate.cfg.xml )
I have some code in the Library to create the session like below
Code:
public static ISessionFactory CurrentSessionFactory
{
get
{
if (HttpContext.Current.Items[CurrentSessionFactoryKey] == null)
{
Configuration config = new Configuration().Configure(HttpContext.Current.Server.MapPath("~/nhibernate.cfg.xml")).SetNamingStrategy(ImprovedNamingStrategy.Instance);
config = AddAssemblies(config);
HttpContext.Current.Items[CurrentSessionFactoryKey] = config.BuildSessionFactory();
}
return (ISessionFactory)HttpContext.Current.Items[CurrentSessionFactoryKey];
}
private set
{
HttpContext.Current.Items[CurrentSessionFactoryKey] = value;
}
}
public static ISession CurrentSession
{
get
{
if (HttpContext.Current.Items[CurrentSessionKey] == null)
{
HttpContext.Current.Items[CurrentSessionKey] = CurrentSessionFactory.OpenSession();
}
return (ISession)HttpContext.Current.Items[CurrentSessionKey];
}
private set
{
HttpContext.Current.Items[CurrentSessionKey] = value;
}
}
private static Configuration AddAssemblies(Configuration config)
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (object attribute in assembly.GetCustomAttributes(true))
{
if (attribute is NHibernateModule)
{
config.AddAssembly(assembly);
break;
}
}
}
return config;
}
Config file (bolded line is the direct mapping i used to use before, which is now removed as i am using lazy loading)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string_name">Default</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
[b]<mapping assembly="Symbonix.BLL" />[/b]
</session-factory>
</hibernate-configuration>
I have business classes that use the session to call simple operations, but when i used to map the assemblies directly in the config files, they worked fine, but when i used to lazy load, it seems the session is created properly as i can see the loaded classes list in it, but when i try to insert/load i get an error saying "Invalid object name '_role'." where Role is my table name i tried with many different tables and they give the same error.
the main exception is "could not load an entity: [Symbonix.BLL.Entities.Role#1][SQL: SELECT role0_.role_id as role1_97_0_, role0_.name as name97_0_ FROM [_role] role0_ WHERE role0_.role_id=?]"
the inner exception is "Invalid object name '_role'."
i tried a lot but not sure what is wrong.