问题
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