我一直在使用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不要映射基類?
我不是。如果你想手動映射你的實體爲什麼你使用'AutoMap'? – 2009-12-08 09:29:11
因爲這是我在文檔中找到的可以排除下面答案中提到的基本類型的唯一方法。你知道任何其他方式通過ClassMap排除基類型嗎? – 2009-12-10 23:16:37