7

I faced the below error fluentNhibernate in .net 6 project but the same code is working in framework 4.8.

Error: could not create the driver from nhibernate.driver.sqlclientdriver, nhibernate, version=5.3.0.0, culture=neutral, publickeytoken=aa95f207798dfdb4

My code in .net 6 is:

public class DatabaseContext
{
    private static ISessionFactory session;

    private static ISessionFactory CreateSession()
    {
        const string connectionString = "Data source=SQLEXPRESS;Database=**;Integrated Security=True";

        if (session != null)
        {
            return session;
        }
        var sqlConfiguration = MsSqlConfiguration.MsSql2012.ConnectionString(connectionString);

        return Fluently.Configure()
            .Database(sqlConfiguration)
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TokenMapping>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(false, true, false))
            .BuildSessionFactory();
    }
    public static NHibernate.ISession SessionOpen()
    {
        return CreateSession().OpenSession();
    }
}

1 Answers1

8

You need to add nuget package System.Data.SqlClient to your project to make NHibernate.Driver.SqlClientDriver work under .NET Core.

Also consider using NHibernate.Driver.MicrosoftDataSqlClientDriver instead (available since NHibernate v5.3) which depends on Microsoft.Data.SqlClient nuget package. Microsoft will favor development of new features in this new NuGet package from now on (see details). In Fluent NHibernate:

var sqlConfiguration = MsSqlConfiguration.
  .MsSql2012
  .Driver<MicrosoftDataSqlClientDriver>() //needs Microsoft.Data.SqlClient nuget package
...

Roman Artiukhin
  • 2,200
  • 1
  • 9
  • 19