0

我正在尝试使用 Fluent NHIbernate 关注这篇文章:http: //blogs.planetcloud.co.uk/mygreatdiscovery/post/Localizing-entities-with-NHibernate.aspx

我的测试产生以下错误:

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

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

添加.IncludeBase<ILocalizedEntity>()到我的汽车模型中什么也没做……(正如预期的那样,它是一个接口而不是抽象的,对吧?)

映射:(问题)

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

翻译文本(有)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 测试套件中也看到了同样的情况。我有一种感觉,这与我使用 AutoMapping 的事实有关,但目前还不确定是什么......

确认编辑 - 使用标准 ClassMaps 而不是自动映射,具有上述相同的映射,按预期工作。

4

1 回答 1

0

我确实找到了一个“解决方法”,即从自动映射中删除所涉及的类,并使用 ClassMaps 手动滚动它们,因为我很喜欢自动映射,所以真的不理想!

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

在 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<>));
}
于 2012-12-31T17:29:05.733 回答