Does ShardMap support Microsoft.Data.SqlClient package ?

Aditi Bhalerao 11 Reputation points
2022-12-27T17:23:28.873+00:00

I have an .net core 6.0 entityframework application and want to connect to SQL Database through Azure Active Directory from the application. The code to connect to SQL Database through Azure Active Directory uses Microsoft.Data.SqlClient as the package. We also have usage of ShardMap in the same DbContext where we are connecting to the database. The ShardMap does not support Microsoft.Data.SqlClient and needs System.Data.SqlClient as the package. Does, ShardMap support Microsoft.Data.SqlClient package ?

Below is piece of code from the DbContext where I am getting the error:

SqlConnection conn = null; //uses Microsoft.Data.SqlClient as the package  
 try{     
  var shardMap = GetShardMapManager(Constants.Settings.ShardMapName, configuration);  
  var connStringBuilder = new SqlConnectionStringBuilder  
  {  
     UserID = userId,  
     Password = password,  
     ApplicationName = "ABC"  
  };  
      
 conn = shardMap.OpenConnectionForKey(shardingKey, connStringBuilder.ConnectionString, ConnectionOptions.Validate);\  
       
  //Gives error at the above line: "Cannot convert source type 'System.Data.SqlClient.SqlConnection' to target type 'Microsoft.Data.SqlClient.SqlConnection'"  
  /* If the above SqlConnection uses System.Data.SqlClient.SqlConnection as the package, the error does not come up. But, in that case, we cannot use Authentication in the GetDatabaseConnection method.  
  */  
  }  
 catch (Exception e)   
 {  
 ......  
 }  

 

private static ShardMap GetShardMapManager(string shardMapName, IConfiguration configuration)  
 {  
     var dbConnectionString = GetDatabaseConnectionString(configuration);  
     var shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager(dbConnectionString, ShardMapManagerLoadPolicy.Lazy);  
     var shardMap = shardMapManager.GetListShardMap<int>(shardMapName);  
     return shardMap;  
 }  

//The method below needs Microsoft.Data.SqlClient to use Authentication=Active Directory Service Principal;

public static string GetDatabaseConnection(IConfiguration config)  
 {  
  return  
                 $"Server=tcp:{config["SERVERNAME"]},1433; Authentication=Active Directory Service Principal; " +  
                 $"Encrypt=True; Database={config[DATABASENAME]}; User Id = {clientId}; Password = {password} ";  
      
  var connection = new SqlConnection(ConnectionString);  
     connection.Open();  
 }  

I want to use just one package among Microsoft.Data.SqlClient and System.Data.SqlClient.SqlConnection. How can it be made that possible?

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
779 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,476 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sami Määttä 1 Reputation point
    2022-12-28T10:30:05.557+00:00

    I'm also waiting for support to Microsoft.Data.SqlClient.
    There's another question about the same issue here: https://learn.microsoft.com/en-us/answers/questions/378770/elastic-scale-client-library-support-and-package-u.html
    doesn't look too promising.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.