NHibernate ищет hibernate.cfg. xml, даже если настроен web.config - PullRequest
0 голосов
/ 19 июня 2020

Я пытаюсь настроить NHibernate в моем ASP. net основном веб-приложении, я слышал, что вы можете настроить NHibernate через web.config, что я сделал, но, похоже, NHibernate все еще ищет спящий режим .cfg. xml. Я видел этот вопрос NHibernate спрашивает hibernate.cfg. xml, даже если web.config настроен , но это не решает эту проблему. Моя версия NHibernate - 5.2.7

web.config

<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="">
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>

      <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
      </property>

      <property name="connection.connection_string">
        <!-- Insert Connection String Here -->
       <add name="db" connectionString="Server=localhost;Database=test;Trusted_Connection=True;" />
      </property>

      <!-- This is specifically for MSSQL 2008 -->
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name='proxyfactory.factory_class'>
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
      </property>


    </session-factory>
  </hibernate-configuration>
</configuration>

startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NHibernate.Cfg;

namespace WebApplication1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            var nhConfig = new Configuration().Configure();
            var sessionFactory = nhConfig.BuildSessionFactory();
            Console.WriteLine("NHibernate Configured!");
            Console.ReadKey();
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}
...