我正在尝试使用 Dapper 在我的 sqlserver 数据库中进行简单的插入。这是我的代码:
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;
这是我的 DapperContext。
public DapperContext(IConfiguration configuration) { _configuration = configuration; _connectionString = _configuration.GetConnectionString("DapperConnection"); } public IDbConnection CreateConnection() => new SqlConnection(_connectionString);
但是当我到达这Execute条线时,我得到了无法加载文件或程序集“Microsoft.SqlServer.Server”。我不知道为什么。我的 SQLServer 由 docker 映像托管。而且我已经安装了服务器 nuget。这是我目前已安装的 nugets 列表:
Execute
<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" />
任何人都可以帮助我这里可能出了什么问题,为什么我不断收到无法加载错误?我什至已经从Microsoft下载了 SQL Server,但我的想法是改用我的 docker 容器。
堆栈跟踪:
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'
我使用了错误版本的 nuget 包。我不得不替换PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.0-preview3.22168.1它PackageReference Include="System.Data.SqlClient" Version="4.8.3"并且它立即起作用
PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.0-preview3.22168.1
PackageReference Include="System.Data.SqlClient" Version="4.8.3"