C# – Fluent NHibernate: How to tell it not to map a base class

asp.net-mvccfluent-nhibernatenhibernate

I have been googling and stackoverflowing for the last two hours and couldn't find an answer for my question:

I'm using ASP.NET MVC and NHibernate and all I'm trying to do is to manually map my entities without mapping its base class. I'm using the following convention:

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; }
}

Where EntityBase SHOULD NOT be mapped.

My NHibernate helper class looks like this:

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();
        }
    }
}

The error that I'm getting now, is:

System.ArgumentException: The type or
method has 2 generic parameter(s), but
1 generic argument(s) were provided. A
generic argument must be provided for
each generic parameter

This happens when I try to add the mappings. Is there any other way to manually map your entities and tell NHibernate not to map a base class?

Best Answer

If you don't want to automap a class, I would recommend using IAutoMappingOverride<T>. I don't about your database, but it might look like:

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
    }
}

Assuming you keep these maps centrally located, you could then load them on your autoMap:

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