Can I use Nhibernate with Docker?

Asked

Viewed 55 times

2

I have a personal project on ASP.NET MVC core that supports Docker. I would like to use Nhibernate to persist and retrieve information. In my searches I have not found anyone who uses. I am trying but the following error occurs:

Internal exception:

Sqlexception: A network-Related or instance-specific error occurred while establishing a Connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote Connections. (Preview: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I call the method that updates or generates the database tables in the program.Cs class

public static void Main(string[] args)
{
    // Chamando o método que Gera ou atualiza as tabelas do banco de dados
    NHibernateHelper.GerarOuAtualizarTabelas();
    // Iniciando o Host Web
   CreateWebHostBuilder(args).Build().Run();
}

The class NHibernateHelper:

public class NHibernateHelper
{
    private static ISessionFactory fabrica = CriaSessionFactory();

    private static ISessionFactory CriaSessionFactory()
    {
        Configuration cfg = RecuperaConfiguracao();
        return cfg.BuildSessionFactory();
    }

    public static Configuration RecuperaConfiguracao()
    {
        Configuration cfg = new Configuration();
        cfg.Configure();
        cfg.AddAssembly(Assembly.GetExecutingAssembly());
        return cfg;
    }

    public static void GerarOuAtualizarTabelas()
    {
        Configuration config = RecuperaConfiguracao();
        new SchemaExport(config).Create(true, true);
    }

    public static ISession AbreSession()
    {

        return fabrica.OpenSession();
    }
}

When switching to run with 'IIS Express', it works normally, I have tried to configure the docker with the help of a few pages to allow remote connections, but was unsuccessful.

  • 1

    Shows an excerpt of the code where the error is generated.

  • I have attached it as a reply. Thank you!

1 answer

2

Henry,

there is no problem with Nhibernate that prevents you from running with Windows Containers or Linux Containers, lately I have been running a lot with Docker on linux.

Your scenario is in windows, however the error you are reporting is not an error in Nhibernate, this is the default message of lack of connectivity in ADO.NET with SQL Server.

I believe your problem is connectionstring.

I guess you don’t quite understand how the network part on Docker works and you’re using a loopback address like localhost or 127.0.0.1 to connect to that bank or using another wrong IP.

Anyway your problem is not in Nhibernate, this is not an Exception of it, the stack trace will assure you about what I am saying.

Revisit the classes on Docker networking and understand the best way to assemble your connectionstring. We have support groups on Telegram.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.