Class library dll throwing exception Could not load file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0 but it reference version 5.2.0

Moshe Yalovsky 5 Reputation points
2024-04-29T20:35:54.9266667+00:00

I have created Class Library using Visual Studio 2022 community v 17.9.6 TragetFramework 8 project file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>.0
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AssemblyName>AchieveResource</AssemblyName>
    <RootNamespace>Achieve.AchieveResource</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.11.0" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Achieve.Common">
      <HintPath>..\Achieve.Common\bin\Debug\net8.0\Achieve.Common.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Compile Update="AchieveResource1.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>AchieveResource1.resx</DependentUpon>
    </Compile>
  </ItemGroup>
   </Project>
 

A console application is referencing this dll and when one of the static methods in the library is called, it throws the following error, although the project is referencing
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" /> it complains that Microsoft.Data.SqlClient version 5.0.0.0 not found.
I got a similar error when using System.Data.SQLClient 4.8.6 and the error was that System.Data.SQLClient version 4.4.0 could not be found.
Any idea? Thanks

FileNotFoundException: Could not load file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'. The system cannot find the file specified.
System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Achieve.AchieveReserouce.ConnectionStrings' threw an exception.
  Source=AchieveResource
  StackTrace:
   at Achieve.AchieveReserouce.ConnectionStrings.GetEnterpriseConnectionString()
   at Program.<Main>$(String[] args) in C:\Dev\Achieve\2022Projects\ConsoleApp1\Program.cs:line 4

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'. The system cannot find the file specified.

The line in the console application is

OUConstr = Achieve.AchieveReserouce.ConnectionStrings.GetConnectionSringForOU(1004);

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,678 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anna Xiu-MSFT 26,561 Reputation points Microsoft Vendor
    2024-04-30T03:40:04.9133333+00:00

    Hi @Moshe Yalovsky, 

    Welcome to Microsoft Q&A! 

    What target framework is your console application using? 

    Please add the Microsoft.Data.SqlClient package as a reference to the console application. 

    Sincerely,

    Anna


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.