0

I really don't like to post such questions like this, trying to look for an error where probably I'm missing something really simple, but I just started learning NHibernate and I'm struggled analysing this for a day now and couldn't find the problem.

My domain class:

namespace PanoMeasurer.Core.Domain
{
    public class Panorama : EntityBase
    {
        public virtual double HeadingBase
        {
            get;
            set;
        }
    }
}

namespace PanoMeasurer.Core.Domain
{
    public class EntityBase : IEntity
    {
        public virtual Guid OID
        {
            get;
            set;
        }
    }
}

My Panorama.hbm.xml mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="PanoMeasurer.Core.Domain" 
                   namespace="PanoMeasurer.Core.Domain">
  <class name="Panorama">
    <id name="OID" generator="guid.comb"/>
    <property name="HeadingBase"/>
  </class>
</hibernate-mapping>

And the code I'm calling in a unit test:

        m_config = new Configuration()
            .SetProperty(Environment.ReleaseConnections, "on_close")
            .SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
            .SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
            .SetProperty(Environment.ConnectionString, "data source =:memory: ")
            .AddFile("Mappings/Xml/Panorama.hbm.xml");

        m_sessionFactory = m_config.BuildSessionFactory();

        Session = m_sessionFactory.OpenSession();

        new SchemaExport(m_config).Execute(true, true, false, Session.Connection, Console.Out);

I'm getting an exception when creating the new configuration:

OneTimeSetUp: NHibernate.MappingException : Could not compile the mapping document: Mappings/Xml/Panorama.hbm.xml ----> System.IO.DirectoryNotFoundException : Não foi possível localizar uma parte do caminho 'C:\Users\Rodrigo\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\Mappings\Xml\Panorama.hbm.xml'. Exception doesn't have a stacktrace

It's says it cannot compile and as far as I understood it's because it couldn't find the xml file on certain path. And why is it looking for the file in the Resharper folder at all??? My projects' output are all set to the default bin/debug folder.

My solution is organized like that:

enter image description here

So you can see that the PanoMeasurer.Core.Domain assembly exists and the Panorama class is located in there.

And my mapping file is set to EmbededResource:

enter image description here

I should also tell that I'm using the lastest Nugget version of Nhibernate.

What am I missing here?

rbasniak
  • 4,484
  • 11
  • 51
  • 100

1 Answers1

1

I found the problem:

1) When running tests with Resharper it uses something called Shadow Copy of Assemblies (Unit testing with Resharper) and copies the assemblies to another folder to run them.

2) The .hbm.xml file was set to Embeded Resource and was being copied to its project output path and Resharper wasn't copying this file with the assembly, so it couldn't be found during the test execution.

To solve, instead of

.AddFile("/Mappings/Xml/Panorama.hbm.xml");

I used

.AddFile(AssemblyLocation() + "/Mappings/Xml/Panorama.hbm.xml");

Where AssemblyLocation is getting the path from CodeBase property:

private string AssemblyLocation()
{
    var codebase = new Uri(Assembly.GetExecutingAssembly().CodeBase);

    return Path.GetDirectoryName(codebase.LocalPath);
}
Community
  • 1
  • 1
rbasniak
  • 4,484
  • 11
  • 51
  • 100