Question

How to connect .NET core to Database cluster instance.

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?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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;
        }
    }