C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – Fluently.从配置文件中配置一个NHibernate数据库?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题类似于 Configuring Fluent NHibernate from NHibernate config section,但我仍然试图将我的大脑包围起来并且答案是不够的.

我想创建一个数据库无关的库存管理应用程序(更像是一个实验而不是其他任何东西).我想使用Fluent自动播放器,因为如果我可以让它工作,它似乎是一个非常酷的主意…保持良好和通用,有点迫使你使用模式.

所以,我使用SessionFactory创建器创建一个Helper类,如下所示:

private static ISessionFactory _SessionFactory;

    public static ISessionFactory SessionFactory {
        get {
            if (_SessionFactory == null) {

                var config = new Configuration().Configure();
                _SessionFactory = Fluently.Configure(config)
                    .Mappings (m => m.AutoMappings.Add (AutoMap.AssemblyOf<Machine> ()))
                    .buildSessionFactory ();
            }
            return _SessionFactory;
        } 
    }

    public static ISession GetSession()
    {
        return SessionFactory.openSession();
    }

我像这样制作一个app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
    </configSections>
    <connectionStrings>
        <add name="NHDataModel" connectionString="Data source=10.10.10.10;Integrated Security=SSPI;Database=Inventory;" />
    </connectionStrings>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
            <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
            <property name="connection.driver_class">NHibernate.Driver.sqlClientDriver</property>
            <property name="dialect">NHibernate.Dialect.Mssql2005Dialect</property>
            <property name="connection.connection_String_name">NHDataModel</property>
            <property name="show_sql">true</property>
        </session-factory>
    </hibernate-configuration>
</configuration>

我尝试生成要使用的会话时收到的错误消息如下:

An invalid or incomplete configuration was used while creaTing a SessionFactory. check PotentialReasons collection,and InnerException for more detail.

  * Database was not configured through Database method.

没有内在的例外.

我认为这有效,但我当然会错.对配置对象的检查表明它已经在应用程序配置中收集了属性信息,但似乎无法将其应用于流畅的数据库方法.我在互联网上看到了与此类似的例子,然没有完全匹配,所以有很多愚蠢错误的空间.

我确信这是一件很简单的事情,但我已经在这几个小时里摸不着头脑了.任何帮助将不胜感激. Nhibernate和FluentNhibernate是我以前从未使用过的技术,所以这是一个陡峭的学习曲线.

解决方法

你的方法似乎很合理;流畅的NHibernate确实应该允许您从配置开始并添加到它.这里是来自Fluent NHibernate站点代码,它在语义上与你的相同,除了你自动化而不是流畅的映射:

var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure(); // read config default style
Fluently.Configure(cfg)
    .Mappings(
      m => m.FluentMappings.AddFromAssemblyOf<Entity>())
    .buildSessionFactory();

我看到的唯一主要区别是网站的代码从未明确地将配置对象变量分配给Configure()方法的结果;这意味着除了返回自身的深度克隆(或仅仅是自身)之外,Configure()还会修改调用它的实例.

要检查的一些事项:您是否确定您使用的Fluent版本引用了使用配置版本2.2的NHibernate版本? FNH 1.2针对NH 3.1,而FNH 1.1针对NH 2.1.2GA.我不知道它们使用哪个配置版本,但如果它们与NH版本匹配,那么Configure()方法可能找不到与它正在使用的ConfigSectionHandler匹配的配置部分.

大佬总结

以上是大佬教程为你收集整理的c# – Fluently.从配置文件中配置一个NHibernate数据库?全部内容,希望文章能够帮你解决c# – Fluently.从配置文件中配置一个NHibernate数据库?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。