简体   繁体   English

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

[英]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.

相关问题 使用城堡ActiveRecord,当两个类具有相同的名称但名称空间不同时,我得到了NHibernate DuplicateMappingException - Using Castle ActiveRecord, I get an NHibernate DuplicateMappingException when two classes have the same name but different namespaces 如何处理具有相同名称的两个类的MVC DisplayTemplates(不同的命名空间) - How to handle MVC DisplayTemplates for two classes with the same name (different namespaces) 如何切换具有相同名称和不同名称空间的类? - How to switch classes with same name and different namespaces? 为什么在映射具有相同名称但不同名称空间的类的对象图时,XmlSerializer无法初始化? - Why is XmlSerializer failing to initialize when mapping the object graph of classes with the same name, but different namespaces? 两个类同名时如何设置默认类 - How to set the default class when two classes have the same name 实体框架代码优先 - 两个具有相同名称但位于不同名称空间的实体 - Entity Framework Code First - two entities with same name but in different namespaces C#当从不同的命名空间中使用两组“相同”类时,如何避免两次写入相同的方法? - C# When consuming two sets of “identical” classes from different namespaces, how do I avoid writing the same method(s) twice? 具有相同对象名称的两个名称空间 - Two namespaces with the same object name 内部类具有相同的命名空间但在不同的程序集中? - Internal classes having the same namespaces but in different assemblies? 如何选择具有相同名称的两个名称空间之一 - How to select one of two namespaces with the same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM