连贯NHibernate:如何告诉它不要映射基类 [英] Fluent NHibernate: How to tell it not to map a base class

查看:218
本文介绍了连贯NHibernate:如何告诉它不要映射基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌上搜索和stackoverflowing最后两个小时,找不到我的问题的答案:

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

我使用ASP.NET MVC和NHibernate和所有我想要做的是手动映射我的实体不映射其基类。我使用以下约定:

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

在哪里EntityBase不应该被映射。

Where EntityBase SHOULD NOT be mapped.

我的NHibernate的辅助类看起来是这样的:

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:类型或
  方法具有2的通用参数(多个),而是
  1一般的参数(S)的规定。一个
  必须提供一般的参数
  每个泛型参数

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

这发生在我尝试添加映射。是否有任何其他的方式来手动映射您的实体,并告诉NHibernate的不就图个基类?

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?

推荐答案

如果你不想自动映射一类,我会建议使用 IAutoMappingOverride&LT; T&GT; 。我没有对你的数据库,但它可能是这样的:

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

这篇关于连贯NHibernate:如何告诉它不要映射基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆