2

I am trying to connect to nHibernate but I did not do that. I have a configuration file which is look like below

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>   
    <property name="connection.connection_string_name">EMS</property>
    <property name="show_sql">false</property>
    <property name="generate_statistics">true</property>
    <property name="current_session_context_class">thread_static</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <property name="cache.use_second_level_cache">false</property>
    <property name="cache.use_query_cache">false</property>
    <property name="default_schema">EMS.dbo</property>
    <property name="adonet.batch_size">200</property>
    <mapping assembly="EMS.Infra.Data"/>
  </session-factory>
</hibernate-configuration>

and have a config file load method like below

//create instance for cfg

configuration = new Configuration(); // read config default style


configuration.AddAssembly(Assembly.GetCallingAssembly());//forcely add mapping assembly

//add fluent auto mapping and conventions
                    Fluently.Configure(configuration).Mappings(m =>
                    m.FluentMappings.AddFromAssemblyOf<Employee>().Conventions.Add(
                        PrimaryKey.Name.Is(x => "Id"),
                        ForeignKey.EndsWith("Id")));

                    //add dialect and connections strting
                    configuration.SessionFactory().Integrate.Using<MsSql2012Dialect>().Connected.ByAppConfing("EMS");//EMS is the connection name

My mapping file

public class EmployeeMap : ClassMap<Employee>
    {
        public EmployeeMap()
        {
            Table("HRM.Employee");
            Id(x => x.Id).GeneratedBy.Identity().UnsavedValue(0);
            Map(x => x.LocationId);
            Map(x => x.SalutationId);
            Map(x => x.FirstName);
            Map(x => x.MiddleName);
            Map(x => x.LastName);
            Map(x => x.NickName);
            Map(x => x.EmpName);
        }
    }

When get data by this method _session.Get(id);

The error has been occurred

An exception of type 'NHibernate.HibernateException' occurred in NHibernate.dll but was not handled in user code

Additional information: Unable to locate persister for the entity named 'EMS.Domain.Hrm.Entities.Employee'.

Please give me any idea that will appreciate.

Shohel
  • 3,886
  • 4
  • 38
  • 75
  • The exception say that the `Employee` is not mapped. Are the `Employee` and `EmployeeMap` in the same assembly? – Rabban Oct 13 '16 at 13:26
  • 1
    Both are not same assembly – Shohel Oct 13 '16 at 13:39
  • 1
    Thats the Problem. Exchange `m.FluentMappings.AddFromAssemblyOf()` with `EmployeeMap` instead of `Employee`, then it should work. – Rabban Oct 13 '16 at 13:47
  • @Rabban, Same error. – Shohel Oct 13 '16 at 14:17
  • Ok, but this is definitely an error. With `m.FluentMappings.AddFromAssemblyOf()` you have to tell him the Assembly of your mappings, not the assembly of your entities. Did you tried to remove the NHibernate config and tried to configure all with FluentNhibernate? Maybe there is some misconfiguration while mixing NHibernateConfig with FluentConfig. – Rabban Oct 14 '16 at 09:01

0 Answers0