Does the nuget package Microsoft.Data.SqlClient work with Entity Framework?
09:39 10 Sep 2020

My application runs on .NET framework 4.7 and I'm using Entity Framework 6.1.3. Currently, my code uses some classes from the namespace System.Data.SqlClient such as SqlParameter. I want to switch to Microsoft.Data.SqlClient.

However, I'm not sure if EF6 is compatible with Microsoft.Data.SqlClient. This is an old article from Microsoft, it says that EF Core, EF 6 etc. haven’t yet made the transition to the new provider Microsoft.Data.SqlClient. So, I'm a bit confused.

Everything has been working well with System.Data.SqlClient for the below code

public async Task> GetChildCustomerIdsAsync(int customerId)
{
   var sqlParameters = new List()
   {
      new SqlParameter("@CustomerId", customerId)
   };

   return await DbContext.Database.SqlQuery("dbo.sp_GetChildCustomerIds @CustomerId=@CustomerId",
                sqlParameters.ToArray()).ToListAsync().ConfigureAwait(false);
}

However, when I am switching to Microsoft.Data.SqlClient, I'm getting this error:

System.InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.
at System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value)
at System.Data.SqlClient.SqlParameterCollection.AddRange(Array values)
at System.Data.Entity.Core.Objects.ObjectContext.CreateStoreCommand(String commandText, Object[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternalAsync.d__6f`1.MoveNext()

c# .net entity-framework entity-framework-6 microsoft-data-sqlclient