Fluent NHibernate: how to say this so as not to display the base class

For the past two hours, I have been searching in googling and stackoverflowing and could not find the answer to my question:

I use ASP.NET MVC and NHibernate, and all I am trying to do is manually map my objects without displaying its base class. I use 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 MUST NOT be displayed.

My NHibernate helper class is as follows:

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 I'm getting now is:

System.ArgumentException: the type or method has 2 common parameters (s), but 1 common argument (s) is provided. a generic argument must be provided for each generic parameter

, . NHibernate ?

+3
4

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

, , autoMap:

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

IncludeBase<T>

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

http://wiki.fluentnhibernate.org/Auto_mapping:)

+7

, , , - :

IgnoreBase<T>, , , Fluent Nhibernate - , , , Fluent Nhibnernate , DefaultAutoMapConfiguration bool ShouldMap(Type type) false, - , .

AutoMapping , , Conventions, . ( Conventions Inspectors)

+1
source

You can use the IsBaseType convention for your autocomplete

0
source

Source: https://habr.com/ru/post/1724906/


All Articles