c# - 流利的 NHibernate : How to tell it not to map a base class

标签 c# asp.net-mvc nhibernate fluent-nhibernate

过去两个小时我一直在使用 google 和 stackoverflowing,但找不到我的问题的答案:

我正在使用 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: 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 不要映射基类?

最佳答案

IncludeBase<T>

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

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

关于c# - 流利的 NHibernate : How to tell it not to map a base class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1858245/

相关文章:

c# - 将 20121004 (yyyyMMdd) 转换为有效的日期时间?

c# - NHibernate 延迟加载 - session 关闭后

c# - 搜索文本包含 QueryOver

c# - NetTcpBinding 和异步/等待 WCF 阻塞

c# - C#获取网页源码的方法

c# - 如何替换 "Error. An error occurred while processing your request."

asp.net-mvc - 如何跟踪来自 MVC 4 内部的内部服务器错误?

nhibernate - NHibernate 中的受歧视联合体

c# - 从资源文件中读取字符串并以编程方式编辑它

c# - MVC Controller HttpStatusCode 上的单元测试不等于