Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from System.Data.SqlClient to Microsoft.Data.SqlClient #17

Merged
merged 1 commit into from
Mar 8, 2022

Conversation

manio143
Copy link
Contributor

In order to support newer authentication methods such as Azure Managed Identities we're looking to migrate to the new SqlClient library. Unfortunately there's no direct compatibility here and changes to manipulations lib are necessary.

In this PR is the simplest change of migrating from one package over to the other.

Is it possible to support both?
For the most part there are 2 points of incompatibility I saw before I started making changes:

  • connection string format change where the new format includes spaces in certain properties (e.g. "Application Intent" and not "ApplicationIntent") - this causes an exception where System.Data.SqlClient.SqlConnectionBuilder was used to retrieve metadata for cache key when adding a new Type for TableValuedParameter. It can be remedied by modifying the connection string before passing it to SqlConnection and removing those spaces for a select few fields (see changed fields).
  • type incompatibility between SqlParameter from the packages - if you want to support both packages at the same time you need to be able to choose which SqlParameter to instantiate depending on the connection type.
    I was able to get a decent fix in ToSqlCommand() extension method (below) but in SqlCommandBuilderExtensions.CreateTableValuedParameter we don't get it as easily.
+            bool useSystemData = queryContext.Connection.DbConnection is System.Data.SqlClient.SqlConnection;
+
-            SqlParameter[] sqlParams = command.Parameters
+            object[] sqlParams = command.Parameters
                 .Where(param => !filterCompositeRelationParameter || !(param is CompositeRelationalParameter))
-                .Select(param => new SqlParameter($"@{param.InvariantName}", parameterValues[param.InvariantName]))
+                .Select(param => useSystemData
+                    ? (object)new System.Data.SqlClient.SqlParameter($"@{param.InvariantName}", parameterValues[param.InvariantName])
+                    : (object)new Microsoft.Data.SqlClient.SqlParameter($"@{param.InvariantName}", parameterValues[param.InvariantName]))
                 .ToArray();

@sebbe33
Copy link
Owner

sebbe33 commented Feb 27, 2022

@manio143 I don't think there's a need to support both packages, as long as the extensions keep on functioning as they did. There seems to be some systemic test failures. Are they due to changes you've made or something in the test infra setup?

@manio143
Copy link
Contributor Author

It's something in the setup

Microsoft.Data.SqlClient.SqlException: Cannot authenticate using Kerberos. Ensure Kerberos has been initialized on the client with 'kinit' and a Service Principal Name has been registered for the SQL Server to allow Kerberos authentication.

Seems it's not using the credentials from Env variables for some reason - but I tested it locally with default Windows auth to SQLExpress and it was working.

@sebbe33 sebbe33 merged commit 5b31a6b into sebbe33:master Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants