在nhibernate中连接数据库时系统找不到文件

时间:2016-10-06 16:43:32

标签: c# sql-server nhibernate fluent-nhibernate

我使用流利的nhibernate很久了。它工作正常,直到我更新我的数据库。之前我使用的是SQL Server 2012,并将其更新到2016年。当尝试在应用程序中连接数据库时,它会抛出错误:

  

系统找不到指定的文件。

当它试图连接时。我的连接功能如下

    Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ShowSql().ConnectionString(x=> x.FromConnectionStringWithKey("imConnectionString2"))).Mappings(m=> m.FluentMappings.AddFromAssemblyOf<MapUsers>()).BuildSessionFactory();

这在数据库更新之前工作正常。我将MsSql2005更改为MsSql2012,但结果相同。

我是否必须在Fluent Nhibernate方面或配置中做任何事情?

请帮助

1 个答案:

答案 0 :(得分:0)

SQL Server 2016的客户端连接发生了重大变化。 这种变化是由于sql客户端正在成为支持windows和其他O.S之类的Linux。

安装Microsoft ODBC Driver 13 for SQL Server - Windows https://www.microsoft.com/en-us/download/details.aspx?id=50420

评论:Installing SQL Server Native Client

SQL Server 2012之外不支持SQL Server Native Client(SNAC)。避免在新的开发工作中使用SNAC,并计划修改当前使用它的应用程序。用于SQL Server的Microsoft ODBC驱动程序提供从Windows到Microsoft SQL Server的本机连接

在配置Nhibernate之前,请确保您可以使用sqlcmd工具连接SQL Server 2016(对于服务器2016-下载)

<强>更新

当您安装名为msodbcsql.msi的驱动程序时,它与sql 2012客户端的驱动程序完全相同。

我在Windows 7(32位)中安装了驱动程序,使用库FluentNHibernate v 2.0.3并连接到SQL Server 2016并成功运行以下代码:

class FluentNHibernateTest
{
    private static ISessionFactory CreateSessionFactory()
    {
       //also MsSqlConfiguration.MsSql2005 is working
        return Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2012.ShowSql()
                        .ConnectionString(x => x.FromConnectionStringWithKey("test16")))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
                .BuildSessionFactory();
    }
    public static string GetSqlVersion()
    {
        string sql = "select @@version version";
        var sessionFactory = CreateSessionFactory();

        using (var session = sessionFactory.OpenSession())
        {
            var query = session.CreateSQLQuery(sql);
            var result = query.UniqueResult();
            Console.WriteLine(result);

            return result.ToString();
        }
    }
}

输出结果:

     FluentNHibernateTest.GetSqlVersion();

  Microsoft SQL Server 2016 (RTM-CU1) (KB3164674) - 13.0.2149.0 (X64)
    Jul 11 2016 22:05:22
    .......