Search code examples
c#sql-serverentity-framework.net-core

Invalid cast error on querying the database


I am developing a console app in .NET Core to read/write in a database, using the same code (with minor changes) that my previous app (for the web, written in ASP.NET MVC).

However, when I query the database, the program (that is running) throws an invalid cast exception

System.InvalidCastException: 'Unable to cast object of type 'Microsoft.Data.SqlClient.SqlCommand' to type 'System.Data.SqlClient.SqlCommand'.'.

But these commands run without issues in my web app.

Could anyone help me to solve this issue?

A .csproj file:

… 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <StartupObject></StartupObject>
  </PropertyGroup>
…
  <ItemGroup>
… 
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.0" />
… 
  </ItemGroup>

My Main routine in Program.cs, where I configure the app:

using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System.Collections.Generic;
… 
            var optionsBuilder = new DbContextOptionsBuilder<fmDataContext>();

            optionsBuilder
                .UseSqlServer(connectionString, providerOptions => providerOptions.CommandTimeout(60));

            using (fmDataContext context = new fmDataContext(optionsBuilder.Options))
… 

My query to the database, where the error occurs (in my data context class):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
… 
            DataTable dt = new DataTable();

            DbConnection connection = Database.GetDbConnection();//)
            using (DbCommand cmd = connection.CreateCommand())
            using (DbDataAdapter sda = new SqlDataAdapter((SqlCommand)cmd))
            {
                cmd.CommandText = sqlQuery;
                cmd.CommandType = CommandType.Text;
                connection.Open();
                rows_returned = sda.Fill(dt);
                connection.Close();
            }

Solution

  • In .NET Core 3.0, the SQL Server-related binaries and its data types were moved out of the native System.Data namespace and into their own dedicated package, this to make the .NET SDK less bloated.

    Those developers who still need to use SQL Server can opt-in by downloading the necessary NuGet package at Microsoft.Data.SqlClient.

    Reference: https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/