Mapping RefrencesAny (multiple tables) with fluent nhibernate

断了今生、忘了曾经 提交于 2019-12-12 04:59:11

问题


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

Any idea how to get NH to honour the interface?

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

Mappings: (Question)

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

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

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

Interface:

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

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

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


回答1:


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

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


来源:https://stackoverflow.com/questions/14000679/mapping-refrencesany-multiple-tables-with-fluent-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!