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

Does this package Microsoft.Data.SqlClient work with Entity Framework? #725

Closed
surajpatil9211 opened this issue Sep 10, 2020 · 8 comments
Closed

Comments

@surajpatil9211
Copy link

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.

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

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

   return await DbContext.Database.SqlQuery<int>("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()

@ErikEJ
Copy link
Contributor

ErikEJ commented Sep 10, 2020

EF 6 is not compatible with Microsoft.Data.SqlClient - one of the issues is that EF6 targets net40, and M.D.S lowest target is netstandrad2.0

@surajpatil9211
Copy link
Author

Thank you @ErikEJ for the clarification. Could you please also tell me if there is any workaround to make the above code work or what would be the recommended approach to switch to Microsoft.Data.SqlClient in my application.

@karinazhou
Copy link
Member

@surajpatil9211 EntityFramework Core internally uses Microsoft.Data.SqlClient so maybe you can switch to that if possible.

@ErikEJ
Copy link
Contributor

ErikEJ commented Sep 10, 2020

EF Core, raw ADO.NET, Dapper...

@JRahnama
Copy link
Member

@ErikEJ Thanks for clarification and support.

@surajpatil9211
Copy link
Author

@ErikEJ @karinazhou Thank you for the clarification.

@daiplusplus
Copy link

For the benefit of others looking into this too, this issue in the EF6 repo is the one to follow: dotnet/ef6#823

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 29, 2021

FYI, I just published a preview package: https://github.com/ErikEJ/EntityFramework6PowerTools/blob/community/README.md#preview-of-ef6-sql-server-provider-based-on-microsoftdatasqlclient

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

No branches or pull requests

5 participants