本文介绍了映射RefreencesAny(多个表)与流利的NHibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Fluent NHIbernate跟踪此帖子: http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Localizing-entities-with-NHibernate.aspx

I am trying to follow this post using Fluent NHIbernate: http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Localizing-entities-with-NHibernate.aspx

我的测试生成以下错误:

My test generates the following error:

  ----> NHibernate.MappingException : An association from the table TranslatedText refers to an unmapped class: .Domain.Localisation.ILocalizedEntity

有人知道如何让NH尊重界面吗?

Any idea how to get NH to honour the interface?

在我的自动模型中添加.IncludeBase<ILocalizedEntity>()并没有执行任何操作...(如预期的那样,它的接口不是抽象的权利吗?)

Adding .IncludeBase<ILocalizedEntity>() to my auto model did nothing... (as expected its an interface not an abstract right?)

映射:(问题)

mapping.HasMany(m => m.TranslatedTexts)
                .AsSet()
                .Inverse().Cascade.SaveUpdate()
                .KeyColumn("EntityId")
                .ForeignKeyConstraintName("none")
                .Where("EntityClass = 'Domain.Question'");

TranslatedText(具有)public virtual ILocalizedEntity Entity { get; set; }

TranslatedText (has) public virtual ILocalizedEntity Entity { get; set; }

mapping.ReferencesAny(tt => tt.Entity)
                .IdentityType<Guid>()
                .EntityIdentifierColumn("EntityId")
                .EntityTypeColumn("EntityType");

接口:

    public interface ILocalizedEntity
    {
        ICollection<TranslatedText> TranslatedTexts { get; set; }
    }

我已经在FNH测试套件中看到了相同的内容.我觉得这与我使用自动映射有关,但还不确定目前该怎么做...

I've seen the same in the FNH test suite. I have a feeling it is something to do with the fact I use AutoMapping, but not sure what as yet...

修改已确认-使用标准的ClassMap而不是自动映射(具有与上面相同的映射)可以正常工作.

editconfirmed - using standard ClassMaps instead of automapping, with the same mappings above, works as expected.

推荐答案

我确实找到了一种解决方法",该解决方法是从自动映射中删除涉及的类,并使用ClassMaps手动滚动它们,这真的很不理想,因为我在进行心脏自动映射!

I did find a "workaround" which was to remove the classes involved from automapping, and hand-roll them using ClassMaps, really not ideal since I HEART automapping!

sessionFactory = Fluently.Configure()
 .Database(sqlConfig)
.Mappings(m => m.AutoMappings.Add(new MyAutoPersistenceModel().GetModel()))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<QuestionMap>())
.BuildSessionFactory();

在MyAutoPersistenceModel中:

In MyAutoPersistenceModel:

public override bool ShouldMap(System.Type type)
{
            var interfaces = type.GetInterfaces();
            return type != typeof(TranslatedText) &&
                !interfaces.Any(x => x.GetType() == typeof(ILocalisedEntity)) &&
                interfaces.Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>));
}

这篇关于映射RefreencesAny(多个表)与流利的NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:51