2011-06-21 55 views
2

我與功能NHibernate以下情況:流利的層次

public class Stuff 
{ 
    public Stuff() 
    { 
     Entities = new List<Entity>(); 
    } 

    public virtual int Id { get; set; } 
    public virtual IList<Entity> Entities { get; set; } 
} 

public abstract class Entity 
{ 
    public virtual int Id { get; set; } 

    public virtual string Type { get; set; } 
    public virtual Stuff Stuff { get; set; } 
} 

public class Person : Entity 
{ 
    public virtual string FirstName { get; set; } 
    public virtual string LastName { get; set; } 
} 

public class Animal : Entity 
{ 
    public virtual string Species { get; set; } 
} 

然後,我有下面的代碼使用自動映射,並生成這些映射:

 var sessionFactory = 
      Fluently.Configure().Database(persistenceConfigurer).Mappings(
       m => 
       m.AutoMappings.Add(
        AutoMap.Source(new Types(typeof(Entity), typeof(Person), typeof(Animal), typeof(Stuff)))) 
        .ExportTo(@"e:\")).ExposeConfiguration(BuildSchema).BuildSessionFactory(); 
然而

,發生的事情是,我得到以下異常:

---> NHibernate.MappingException:關聯引用未映射的類:ConsoleApplication1.Models.Entity

,如果我做實體類非抽象但是這個作品,我想,以避免在數據庫中的表,但仍維持層次概念,重新

回答

2

您需要添加自動映射這樣的

AutoMap.AssemblyOf<Entity>(yourConfiguration).IgnoreBase<Entity>(); 

這不僅會忽略你的Entity基類,但你並不需要添加每個實體手動,只要每個模型從Entity繼承。