【问题标题】:Mapping RefrencesAny (multiple tables) with fluent nhibernate使用流利的 nhibernate 映射 RefrencesAny(多个表)
【发布时间】:2012-12-09 15:44:31
【问题描述】:

我正在尝试使用 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 而不是自动映射,具有上述相同的映射,按预期工作。

【问题讨论】:

    标签: fluent-nhibernate mapping


    【解决方案1】:

    我确实找到了一个“解决方法”,即从自动映射中删除所涉及的类,并使用 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<>));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 2010-12-26
      • 2010-10-25
      • 1970-01-01
      相关资源
      最近更新 更多