2009-12-07 66 views
3

我一直在使用Google和stackoverflowing最後兩個小時,找不到我的問題的答案:流利的NHibernate:如何告訴它不映射基類

我使用ASP.NET MVC和NHibernate,我試圖做的是手動映射我的實體,而不映射其基類。我使用以下約定:

public class Car : EntityBase 
{ 
    public virtual User User { get; set; } 
    public virtual string PlateNumber { get; set; } 
    public virtual string Make { get; set; } 
    public virtual string Model { get; set; } 
    public virtual int Year { get; set; } 
    public virtual string Color { get; set; } 
    public virtual string Insurer { get; set; } 
    public virtual string PolicyHolder { get; set; } 
} 

其中EntityBase不應映射。

我NHibernate的輔助類看起來是這樣的:

namespace Models.Repository 
{ 
    public class NHibernateHelper 
    { 
     private static string connectionString; 
     private static ISessionFactory sessionFactory; 
     private static FluentConfiguration config; 

     /// <summary> 
     /// Gets a Session for NHibernate. 
     /// </summary> 
     /// <value>The session factory.</value> 
     private static ISessionFactory SessionFactory 
     { 
      get 
      { 
       if (sessionFactory == null) 
       { 
        // Get the connection string 
        connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; 
        // Build the configuration 
        config = Fluently.Configure().Database(PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString)); 
        // Add the mappings 
        config.Mappings(AddMappings); 
        // Build the session factory 
        sessionFactory = config.BuildSessionFactory(); 
       } 
       return sessionFactory; 
      } 
     } 

     /// <summary> 
     /// Add the mappings. 
     /// </summary> 
     /// <param name="mapConfig">The map config.</param> 
     private static void AddMappings(MappingConfiguration mapConfig) 
     { 
      // Load the assembly where the entities live 
      Assembly assembly = Assembly.Load("myProject"); 
      mapConfig.FluentMappings.AddFromAssembly(assembly); 
      // Ignore base types 
      var autoMap = AutoMap.Assembly(assembly).IgnoreBase<EntityBase>().IgnoreBase<EntityBaseValidation>(); 
      mapConfig.AutoMappings.Add(autoMap); 
      // Merge the mappings 
      mapConfig.MergeMappings(); 
     } 

     /// <summary> 
     /// Opens a session creating a DB connection using the SessionFactory. 
     /// </summary> 
     /// <returns></returns> 
     public static ISession OpenSession() 
     { 
      return SessionFactory.OpenSession(); 
     } 

     /// <summary> 
     /// Closes the NHibernate session. 
     /// </summary> 
     public static void CloseSession() 
     { 
      SessionFactory.Close(); 
     } 
    } 
} 

的錯誤,現在我得到是:

System.ArgumentException:類型或 方法有2泛型參數( s),但提供了 1個通用參數。必須爲每個 泛型參數

發生這種情況時,我嘗試添加這些映射 一般的參數。有沒有其他的方法來手動映射你的實體,並告訴NHibernate不要映射基類?

+0

我不是。如果你想手動映射你的實體爲什麼你使用'AutoMap'? – 2009-12-08 09:29:11

+0

因爲這是我在文檔中找到的可以排除下面答案中提到的基本類型的唯一方法。你知道任何其他方式通過ClassMap排除基類型嗎? – 2009-12-10 23:16:37

回答

3

如果你不想自動映射一類,我會建議使用IAutoMappingOverride<T> 。我不知道你的數據庫,但它可能看起來像:

public class CarOverride : IAutoMappingOverride<Car> 
{ 

    public void Override(AutoMapping<Car> mapping){ 
     mapping.Id(x => x.Id, "CarId") 
      .UnsavedValue(0) 
      .GeneratedBy.Identity(); 


     mapping.References(x => x.User, "UserId").Not.Nullable(); 

     mapping.Map(x => x.PlateNumber, "PlateNumber"); 
     // other properties 
    } 
} 

假設你把這些地圖中央的位置,然後你可以加載它們在你的自動地圖:

var autoMap = AutoMap.Assembly(assembly).IgnoreBase<EntityBase>().IgnoreBase<EntityBaseValidation>() 
       .UseOverridesFromAssemblyOf<CarOverride>(); 
+0

喲......這真的很醜。我應該能夠告訴我的類映射this.IgnoreBase();並且配置應該可以在沒有100英里的情況下直接運行 – 2010-01-23 12:38:23

0

可以使用IsBaseType約定您automappings

+1

當我想手動映射它們時呢?正如我所說,我不想使用自動映射...理想情況下,我想刪除自動映射位,並手動映射我所有的實體。但是,我不想映射我的基類。 – 2009-12-07 10:55:48

1

我知道這是一個老的問題,但我認爲有些事情是缺少在這裏:

當您使用IgnoreBase<T>你告訴你不想繼承映射到你的數據庫,但Fluent Nhibernate仍會基類映射爲一個單獨的類,而你不」不要告訴它所以如果你想告訴Fluent Nhibnernate不要映射類本身,你應該繼承DefaultAutoMapConfiguration類並覆蓋它的​​,如果類型是你根本不想映射的類型,則返回false。

當您使用AutoMapping時,通常您不需要任何其他映射類或覆蓋,除非您想對映射進行更改,並且使用Conventions不可能這樣做,或者您只想覆蓋一個類的一小部分(雖然你可以用ConventionsInspectors來做同樣的事情)