Home > Software design >  Could not load file or assembly 'Microsoft.SqlServer.Server'
Could not load file or assembly 'Microsoft.SqlServer.Server'

Time:07-06

I am trying to make a simple insert into my sqlserver database using Dapper. This is my code:

        using var db = DbContext.CreateConnection();
        db.Open();
        string sqlQuery =
            "INSERT INTO Account (Id, Balance, NumberOfTransactions) VALUES(@Id, @Balance, @NumberOfTransactions)";
        int rowsAffected = db.Execute(sqlQuery, entity);
        return rowsAffected > 0;

And this is my DapperContext.

public DapperContext(IConfiguration configuration)
{
    _configuration = configuration;
    _connectionString = _configuration.GetConnectionString("DapperConnection");
}

public IDbConnection CreateConnection()
    => new SqlConnection(_connectionString);

But when I reach the Execute line I am getting Could not load file or assembly 'Microsoft.SqlServer.Server'. I have no idea why. My SQLServer is being hosted by a docker image. And I've already installed the Server nuget. Here's the list of nugets that I currently have installed:

    <PackageReference Include="Akka" Version="1.4.39" />
    <PackageReference Include="Akka.DependencyInjection" Version="1.4.39" />
    <PackageReference Include="Akka.Remote" Version="1.4.39" />
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="Dapper.EntityFramework" Version="2.0.90" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.0-preview3.22168.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.5.22302.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-preview.5.22302.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-preview.5.22302.2" />
    <PackageReference Include="Microsoft.SqlServer.Server" Version="1.0.0" />
    <PackageReference Include="Microsoft.SqlServer.Types" Version="160.600.9-ctp2p0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />

Anyone can help me what might be wrong here and why do I keep getting this could not load error? I even already download SQL Server from Microsoft but my idea was to use my docker container instead.

Stack trace:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.SqlServer.Server, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'. The system cannot find the file specified.
File name: 'Microsoft.SqlServer.Server, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'

CodePudding user response:

I was using the wrong version of the nuget package. I had to replace PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.0-preview3.22168.1 with PackageReference Include="System.Data.SqlClient" Version="4.8.3" and it worked right away

  • Related