28

I have a class in my domain model root that looks like this:

namespace Domain
{
  public class Foo { ... }
}

I also have another class with the same name in a different namespace:

namespace Domain.SubDomain
{
  public class Foo { ... }
}

For my mappings, I have a Mapping directory with a subdirectory called SubDomain that contains mappings for the domain classes found in Domain.SubDomain namespace. They are all in the same assembly.

However, when I try to load them with NHibernate, I keep getting a DuplicateMappingException... even though both Foos having different namespaces. The code I am using to load my NHibernate configuration is this:

var cfg = new Configuration()
  .Configure()                
  .AddAssembly("Domain");   

How can I tell NHibernate to let me use two entities with the same name (but different namespaces)?

cdmckay
  • 31,832
  • 25
  • 83
  • 114
  • In case it helps anyone: same question for Fluent.nHibernate: https://stackoverflow.com/questions/1290466/ – Malcolm Dec 09 '17 at 00:40

3 Answers3

27

I found the answer on the Hibernate website:

If you have two persistent classes with the same unqualified name, you should set auto-import="false". An exception will result if you attempt to assign two classes to the same "imported" name.

I used that as an attribute for the <hibernate-mapping> tag and it worked.

Michael
  • 8,362
  • 6
  • 61
  • 88
cdmckay
  • 31,832
  • 25
  • 83
  • 114
19

I have had the same problem. I solved it like this:

Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(...)
                .AdoNetBatchSize(500))
            .Mappings(m => m.FluentMappings
                .Conventions.Setup(x => x.Add(AutoImport.Never()))
                .AddFromAssembly(...)
                .AddFromAssembly(...)
                .AddFromAssembly(...)
                .AddFromAssembly(...))
            ;

The imported part is: .Conventions.Setup(x => x.Add(AutoImport.Never())). Everything seems to be working fine with this configuration.

TiltonJH
  • 441
  • 4
  • 6
3

You can specify a classes fully qualified name in the mapping document like so:

<class name="SeeMe.Data.People.Relationship, SeeMe.Data" ... > ...

Where SeeMe.Data is the assembly.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
  • 1
    Still says: `NHibernate.DuplicateMappingException: duplicate import: Foo refers to both Domain.SubDomain.Foo, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null and Domain.Foo, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null (try using auto-import="false")` – cdmckay Jul 20 '09 at 22:41
  • 1
    Dammit, soon as I copied and pasted that I saw the `auto-import="false"` suggestion... and it worked. – cdmckay Jul 20 '09 at 22:44
  • 1
    hehe saight, nHibernate rocks but I feel like it's not very intuitive. I've spent a lot of time banging my head over some silly mapping issue. – Spencer Ruport Jul 20 '09 at 22:59