Page 1 of 1

Is there a way to specify port on the connection string?

Posted: Wed 01 Feb 2017 23:47
by OutOfTouch6947
I was wondering if there is a way to specify port the connection string?

I have another question not sure if it belongs here or EntityDeveloper section.

I just converted an application that was using EntityDeveloper with LinqToSql to use EnityDeveloper and DotConnect for PostgreSQL.

My app stores the connection string enrypted in the web.config and it was retrieving it and decrypting it and setting the base Connection objects connect string to this value in the DataContext OnCreated() method, I created my own partial class to do this so that when the model regenerated I don't lose this code. Problem is this Connection in old code was of type Devart.Data.Linq.DataContext.Connection this now needs to be PgSqlConnection but I don't see where this is created so that I can do the same as before.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using Stauer.Utilities.Encryption;

namespace DigitalPaperOrderContext
{
    public partial class DigitalPaperOrderDataContext
    { 

        partial void OnCreated()
        {
            //Decrypt the connection string here and set the base connection string to this value.
            string pwd = "somePassword";
            string connString = WebConfigurationManager.ConnectionStrings["DigitalPaperOrderEncrypted"].ConnectionString;
            connString = AESGCM.SimpleDecryptWithPassword(connString, pwd);
            Connection.ConnectionString = connString;
            this.CommandTimeout = 120;
        }

    }
}
I am not sure if this code will still work or how DevArts code interjects it's PgSqlConnection in place of the Devart.Data.Linq.DataContext.Connection object.

Re: Is there a way to specify port on the connection string?

Posted: Thu 02 Feb 2017 10:02
by Shalex
OutOfTouch6947 wrote:I was wondering if there is a way to specify port the connection string?
1. Please set the port number via the Port connection string parameter: https://www.devart.com/dotconnect/postg ... tring.html.
OutOfTouch6947 wrote:Problem is this Connection in old code was of type Devart.Data.Linq.DataContext.Connection this now needs to be PgSqlConnection but I don't see where this is created so that I can do the same as before.
2. The question is not clear.

LINQ to SQL template works with System.Data.Linq.DataContext, its metadata:

Code: Select all

namespace System.Data.Linq {
    public class DataContext : IDisposable {
        public DataContext(string fileOrServerOrConnection);
        public DataContext(IDbConnection connection);
        public DataContext(string fileOrServerOrConnection, MappingSource mapping);
        public DataContext(IDbConnection connection, MappingSource mapping);

        public ChangeConflictCollection ChangeConflicts { get; }
        public int CommandTimeout { get; set; }
        public DbConnection Connection { get; }
[...]
LinqConnect template works with Devart.Data.Linq.DataContext, its metadata:

Code: Select all

namespace Devart.Data.Linq {
    public class DataContext : IDisposable {
        public DataContext(IDbConnection connection);
        public DataContext(string persisterConfigData);
        public DataContext(IDbConnection connection, MappingSource mapping);
        public DataContext(string connectionString, MappingSource mapping);

        public ChangeConflictCollection ChangeConflicts { get; }
        public int CommandTimeout { get; set; }
        public DbConnection Connection { get; }
[...]
Constructors of both DataContexts accept connectionString of the System.String type. Why can't you pass connection string to the constructor of Devart.Data.Linq.DataContext? If you receive some error, please specify its exact text and full stack trace.

Re: Is there a way to specify port on the connection string?

Posted: Thu 02 Feb 2017 16:06
by OutOfTouch6947
Shalex wrote:
OutOfTouch6947 wrote:I was wondering if there is a way to specify port the connection string?
1. Please set the port number via the Port connection string parameter: https://www.devart.com/dotconnect/postg ... tring.html.
OutOfTouch6947 wrote:Problem is this Connection in old code was of type Devart.Data.Linq.DataContext.Connection this now needs to be PgSqlConnection but I don't see where this is created so that I can do the same as before.
2. The question is not clear.

LINQ to SQL template works with System.Data.Linq.DataContext, its metadata:

Code: Select all

namespace System.Data.Linq {
    public class DataContext : IDisposable {
        public DataContext(string fileOrServerOrConnection);
        public DataContext(IDbConnection connection);
        public DataContext(string fileOrServerOrConnection, MappingSource mapping);
        public DataContext(IDbConnection connection, MappingSource mapping);

        public ChangeConflictCollection ChangeConflicts { get; }
        public int CommandTimeout { get; set; }
        public DbConnection Connection { get; }
[...]
LinqConnect template works with Devart.Data.Linq.DataContext, its metadata:

Code: Select all

namespace Devart.Data.Linq {
    public class DataContext : IDisposable {
        public DataContext(IDbConnection connection);
        public DataContext(string persisterConfigData);
        public DataContext(IDbConnection connection, MappingSource mapping);
        public DataContext(string connectionString, MappingSource mapping);

        public ChangeConflictCollection ChangeConflicts { get; }
        public int CommandTimeout { get; set; }
        public DbConnection Connection { get; }
[...]
Constructors of both DataContexts accept connectionString of the System.String type. Why can't you pass connection string to the constructor of Devart.Data.Linq.DataContext? If you receive some error, please specify its exact text and full stack trace.
Can you provide me an example of overriding the constructor and setting the connection string in a partial class.

Re: Is there a way to specify port on the connection string?

Posted: Fri 03 Feb 2017 15:15
by Shalex
There is no way to override a constructor from a partial class. Please define a constructor with a new set of parameter(s), for example:

Code: Select all

    class Program {
        static void Main(string[] args) {
            using (var connection = new Devart.Data.PostgreSql.PgSqlConnection()) {
                connection.ConnectionString = your_method_to_get_decripted_connection_string();

                using (var context = new YourDataContext(connection)) {

                    var a = context.Depts.ToList();
                }
            }
        }
    }

    public partial class YourDataContex : Devart.Data.Linq.DataContext {
        public YourDataContex(Devart.Data.PostgreSql.PgSqlConnection conn) : base(conn) { }
    }

Re: Is there a way to specify port on the connection string?

Posted: Tue 07 Feb 2017 16:09
by OutOfTouch6947
Shalex wrote:There is no way to override a constructor from a partial class. Please define a constructor with a new set of parameter(s), for example:

Code: Select all

    class Program {
        static void Main(string[] args) {
            using (var connection = new Devart.Data.PostgreSql.PgSqlConnection()) {
                connection.ConnectionString = your_method_to_get_decripted_connection_string();

                using (var context = new YourDataContext(connection)) {

                    var a = context.Depts.ToList();
                }
            }
        }
    }

    public partial class YourDataContex : Devart.Data.Linq.DataContext {
        public YourDataContex(Devart.Data.PostgreSql.PgSqlConnection conn) : base(conn) { }
    }
I must have misunderstood you before when you said use the constructor I thought you implied overriding it which I also know there is no way to do that from a partial class.
This approach you suggested here is good, I will consider changing to it from what I am currently doing in the OnCreated() method.
Thank You for the help.