views:

582

answers:

3

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

No the error that I'm getting 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?

A: 

You can use IsBaseType convention for your automappings

Sly
What about when I want to manually map them? As I metioned, I don't wanna use auto mappings ... Ideally I want to remove the auto mappings bits and manually map all my entities. However, I don't want to map my base classes.
afgallo
+3  A: 

IncludeBase<T>

AutoMap.AssemblyOf<Entity>()
  .IgnoreBase<Entity>()
  .Where(t => t.Namespace == "Entities");

Read more here http://wiki.fluentnhibernate.org/Auto%5Fmapping :)

Kenny Eliasson
+1  A: 

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>
{
    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 keet these maps centrally located, you could then load them on your autoMap:

var autoMap = AutoMap.Assembly(assembly).IgnoreBase<EntityBase>().IgnoreBase<EntityBaseValidation>()
                .UseOverridesFromAssemblyOf<CarOverride>();
ddango
yuk ... that's really ugly. I should be able to tell my class mapping that this.IgnoreBase(); and the configuration should work directly without 100 miles turning around
jalchr