NHibernate的DuplicateMappingException当两个类具有相同的名称,但不同的命名空间不同、名称、两个、空间

2023-09-04 07:00:43 作者:该网名回复后可见

我有一类在我的域模型的根,看起来是这样的:

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 { ... }
}

有关我的映射,我有一个映射有一个名为的子目录子域包含域类映射目录在 Domain.SubDomain 命名空间中。他们都是在同一个组件。

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.

然而,当我尝试加载它们与NHibernate,我不断收到一个 DuplicateMappingException ...即使同时具有FOOS不同的命名空间。在code,我使用加载我的NHibernate的配置是这样的:

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");

怎么能告诉NHibernate的让我用两个实体名称相同(但不同的命名空间)?

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

推荐答案

我找到了answer Hibernate的网站:

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.