代码之家  ›  专栏  ›  技术社区  ›  Andre Gallo

Fluent NHibernate:如何告诉它不要映射基类

  •  3
  • Andre Gallo  · 技术社区  · 15 年前

    在过去的两个小时里,我一直在咕噜咕噜地堆着水,找不到我的问题的答案:

    我正在使用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个泛型参数,但 提供了1个通用参数。一 必须为提供泛型参数 每个通用参数

    当我尝试添加映射时会发生这种情况。有没有其他方法可以手动映射实体并告诉nhibernate不要映射基类?

    4 回复  |  直到 15 年前
        1
  •  3
  •   harriyott Erik Funkenbusch    11 年前

    如果您不想自动映射类,我建议使用 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>();
    
        2
  •  7
  •   Kenny Eliasson    15 年前

    IncludeBase<T>

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

    在这里阅读更多 http://wiki.fluentnhibernate.org/Auto_mapping :)

        3
  •  1
  •   Beatles1692    9 年前

    我知道这是一个古老的问题,但我认为这里缺少一些东西:

    当你使用 IgnoreBase<T> 您告诉我们您不想将继承映射到数据库中,但是 Fluent Nhibernate 仍然将您的基类映射为单个类,而您不告诉它不要这样做,所以如果您想告诉 Fluent Nhibnernate 不映射类本身,应继承 DefaultAutoMapConfiguration 类并重写其 bool ShouldMap(Type type) 如果类型是任何您根本不想映射的类型,则返回false。

    当你使用 AutoMapping 一般来说,您不需要任何其他映射类或重写,除非您希望更改映射,并且不可能使用 Conventions 或者您只想重写一个类的一小部分。(尽管您可以使用 习俗 Inspectors )

        4
  •  0
  •   Sly    15 年前

    您可以对自动应用使用ISbaseType约定