首页 > 解决方案 > 如何在代码中配置 NHibernate SessionFactory

问题描述

我是 NHibernate 的新手,我正在尝试根据“Learning NHibernate 4”一书对其进行配置。但是,我不知道如何配置它。我有一个类,Connection但是当我尝试使用它时,NHibernate 告诉我它找不到“HbmMapping”。

class Connection
    {
        public Connection() 
        {
            var cfg = new Configuration();
            cfg.DataBaseIntegration(x =>
            {
                x.Dialect<PostgreSQLDialect>();
                x.Driver<NpgsqlDriver>();
                x.ConnectionString = "Server=127.0.0.1; Port=5433; User Id=smartwarehouse; Password=$smart#2018;Database=warehouse;";
                x.ConnectionReleaseMode = ConnectionReleaseMode.OnClose;
                x.LogSqlInConsole = true;
                x.LogFormattedSql = true;


            }).AddMapping(GetMappings());

        }
          // here is Hbm dosn't find from library
     private HbmMapping GetMappings() 
        {



        }
    }

在此处输入图像描述

它给了我其他两个选项可以像这里一样使用

在此处输入图像描述

标签: c#nhibernatefluent-nhibernate

解决方案


可能是解决此问题的更好资源。您通常会告诉它您的映射在装配级别的位置......

.AddFromAssemblyOf<YourEntity>();

...因此,当您添加/删除映射时,您无需更改代码。

例如,我SessionProvider的有点像这样:

Config = new NHibernateConfig();
Config.Configure(); // read config default style

Fluently
     .Configure(Config)
     .Mappings(
         m => m.FluentMappings.AddFromAssemblyOf<UserMap>()
...

我没有.hbm文件,因为我使用ClassMap. 但是,只要您在AddFromAssemblyOf方法中指定的类型与您的文件在同一个程序集中.hbm,它就应该可以工作。所以像:

Fluently
    .Configure(Config)
    .Mappings(
        m => m.HbmMappings.AddFromAssemblyOf<ATypeInYourMappingAssembly>()

推荐阅读