Question

How to connect .NET core to Database cluster instance.

Posted April 18, 2020 286 views
Configuration Management

Hi guys,

I was working through one of my development projects and hit an obstacle with regards to using connection strings for .NET core to database managed instance. Is there any recommended solutions for direct connection to digital ocean DB cluster instance without PEM cert and key or alternatively, adding the cert and key into the application instead?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi!

I use a class like the following for connection to the database. If you want you can add the certificate to your application. Otherwise you must set the Field TrustServerSideCertificate to true

public class DatabaseSettings
    {
        public string Hostname { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public bool DisableSSL { get; set; }
        public int Port { get; set; } = 5432;
        public bool TrustServerCertificate { get; set; } = false;

        public string GetConnectionString()
        {
            var applicationName = Assembly.GetEntryAssembly().FullName;

            var connectionBuilder = new NpgsqlConnectionStringBuilder
            {
                Host = Hostname,
                Database = Name,
                Username = Username,
                Password = Password,
                Port = Port,
                MaxPoolSize = 50,
                ApplicationName = applicationName,
                TrustServerCertificate = TrustServerCertificate,
                SslMode = DisableSSL ? SslMode.Disable : SslMode.Require,
            };

            return connectionBuilder.ConnectionString;
        }
    }
Submit an Answer