映射抽象类和子类时NHibernate的DuplicateMappingException [英] NHibernate DuplicateMappingException when mapping abstract class and subclass

查看:214
本文介绍了映射抽象类和子类时NHibernate的DuplicateMappingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类和这个的子类,我想用NHibernate把它映射到我的数据库。我正在使用Fluent,并且在wiki上阅读如何进行映射。但是当我添加子类的映射时,映射时会抛出一个NHibernate.DuplicateMappingException异常。为什么?



以下是我的(简化)类:

  public abstract class FieldValue 
{
public int Id {get;组; }
public abstract object Value {get;组; }
}

public class StringFieldValue:FieldValue
{
public string ValueAsString {get;组; }
public override object Value
{
get
{
return ValueAsString;
}
set
{
ValueAsString =(string)value;





映射: p>

  public class FieldValueMapping:ClassMap< FieldValue> 
{
public FieldValueMapping()
{
Id(m => m.Id).GeneratedBy.HiLo(1);
// DiscriminateSubClassesOnColumn(type);
}
}

public class StringValueMapping:SubclassMap< StringFieldValue>
{
public StringValueMapping()
{
Map(m => m.ValueAsString).Length(100);






$例外


NHibernate.MappingException:无法编译映射文档:(XmlDocument)
----> NHibernate.DuplicateMappingException:重复的类/实体映射NamespacePath.StringFieldValue

有什么想法?

解决方案

发现问题。事实证明,我在用于配置数据库的PersistenceModel中多次引用了相同的程序集:

  public class MappingsPersistenceModel:PersistenceModel 
{
public MappingsPersistenceModel()
{
AddMappingsFromAssembly(typeof(FooMapping).Assembly);
AddMappingsFromAssembly(typeof(BarMapping).Assembly);
// FooMapping和BarMapping在同一个程序集中。




$ b $ p $显然这对于​​ClassMap映射来说不是问题。但是对于SubclassMap来说,它不能很好地处理它,从而导致重复的映射,从而导致DuplicateMappingException异常。删除PersistenceModel中的重复项可以解决问题。

I have an abstract class, and subclasses of this, and I want to map this to my database using NHibernate. I'm using Fluent, and read on the wiki how to do the mapping. But when I add the mapping of the subclass an NHibernate.DuplicateMappingException is thrown when it is mapping. Why?

Here are my (simplified) classes:

public abstract class FieldValue
{
    public int Id { get; set; }
    public abstract object Value { get; set; }
}

public class StringFieldValue : FieldValue
{        
    public string ValueAsString { get; set; }
    public override object Value
    {
        get
        {
            return ValueAsString; 
        } 
        set
        {
            ValueAsString = (string)value; 
        }
    } 
}

And the mappings:

public class FieldValueMapping : ClassMap<FieldValue>
{
    public FieldValueMapping()
    {
        Id(m => m.Id).GeneratedBy.HiLo("1");
        // DiscriminateSubClassesOnColumn("type"); 
    }
}

public class StringValueMapping : SubclassMap<StringFieldValue>
{
    public StringValueMapping()
    { 
        Map(m => m.ValueAsString).Length(100);
    }
}

And the exception:

NHibernate.MappingException : Could not compile the mapping document: (XmlDocument) ----> NHibernate.DuplicateMappingException : Duplicate class/entity mapping NamespacePath.StringFieldValue

Any ideas?

解决方案

Discovered the problem. It turned out that I did reference the same Assembly several times in the PersistenceModel used to configure the database:

public class MappingsPersistenceModel : PersistenceModel
{
    public MappingsPersistenceModel()
    {
        AddMappingsFromAssembly(typeof(FooMapping).Assembly);
        AddMappingsFromAssembly(typeof(BarMapping).Assembly);
        // Where FooMapping and BarMapping is in the same Assembly. 
    }
}

Apparently this is not a problem for ClassMap-mappings. But for SubclassMap it doesn't handle it as well, causing duplicate mappings - and hence the DuplicateMappingException. Removing the duplicates in the PersistenceModel fixes the problem.

这篇关于映射抽象类和子类时NHibernate的DuplicateMappingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆