[英]NHibernate DuplicateMappingException when two classes have the same name but different namespaces
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.对于我的映射,我有一个
Mapping
目录,其中有一个名为SubDomain
的子目录,其中包含Domain.SubDomain
命名空间中的域类的映射。 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.但是,当我尝试使用 NHibernate 加载它们时,我不断收到
DuplicateMappingException
......即使两个 Foos 都有不同的命名空间。 The code I am using to load my NHibernate configuration is this:我用来加载我的 NHibernate 配置的代码是这样的:
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)?我如何告诉 NHibernate 让我使用两个具有相同名称(但名称空间不同)的实体?
I found the answer on the Hibernate website:我在 Hibernate 网站上找到了答案:
If you have two persistent classes with the same unqualified name, you should set
auto-import="false"
.如果您有两个具有相同非限定名称的持久类,则应设置
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.我用它作为
<hibernate-mapping>
标签的一个属性并且它起作用了。
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()))
.导入的部分是:
.Conventions.Setup(x => x.Add(AutoImport.Never()))
。 Everything seems to be working fine with this configuration.使用此配置似乎一切正常。
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.其中 SeeMe.Data 是程序集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.