加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Fluently.从配置文件中配置一个NHibernate数据库?

发布时间:2020-12-16 01:48:21 所属栏目:百科 来源:网络整理
导读:这个问题类似于 Configuring Fluent NHibernate from NHibernate config section,但我仍然试图将我的大脑包围起来并且答案是不够的. 我想创建一个与数据库无关的库存管理应用程序(更像是一个实验而不是其他任何东西).我想使用Fluent自动播放器,因为如果我可
这个问题类似于 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匹配的配置部分.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读