gpt4 book ai didi

dapper - 如何使用 Dapper 传递空参数

转载 作者:行者123 更新时间:2023-12-03 08:36:24 27 4
gpt4 key购买 nike

我有一个存储过程,它有一个没有默认值的参数,但它可以为空。但我不知道如何使用 Dapper 传递 null。我可以在 ADO 中做得很好。

connection.Execute("spLMS_UpdateLMSLCarrier", new { **RouteId = DBNull.Value**, CarrierId = carrierID, UserId = userID, TotalRouteRateCarrierId = totalRouteRateCarrierId },
commandType: CommandType.StoredProcedure);

异常(exception):
System.NotSupportedException was caught
Message=The member RouteId of type System.DBNull cannot be used as a parameter value
Source=Dapper
StackTrace:
at Dapper.SqlMapper.LookupDbType(Type type, String name) in C:\Dev\dapper-git\Dapper\SqlMapper.cs:line 348
at Dapper.SqlMapper.CreateParamInfoGenerator(Identity identity) in C:\Dev\dapper-git\Dapper\SqlMapper.cs:line 1251
at Dapper.SqlMapper.GetCacheInfo(Identity identity) in C:\Dev\dapper-git\Dapper\SqlMapper.cs:line 908
at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\Dev\dapper-git\Dapper\SqlMapper.cs:line 532
at Rating.Domain.Services.OrderRatingQueueService.UpdateLMSLCarrier(Int32 totalRouteRateCarrierId, Int32 carrierID, Int32 userID) in C:\DevProjects\Component\Main\Source\Rating\Source\Rating.Domain\Services\OrderRatingQueueService.cs:line 52
at Benchmarking.Program.OrderRatingQueue() in C:\DevProjects\Component\Main\Source\Benchmarking\Source\Benchmarking\Program.cs:line 81
at Benchmarking.Program.Main(String[] args) in C:\DevProjects\Component\Main\Source\Benchmarking\Source\Benchmarking\Program.cs:line 28
InnerException:

不能关闭 RouteId,给我一个错误,说它是必需的。不能使用常规 null 也给我另一个错误。不能更改存储过程,它不属于我。

最佳答案

我认为你应该可以通过类型转换 null 来做到这一点。到合适的类型。让我们假设 RouteId 是一个整数:

connection.Execute("spLMS_UpdateLMSLCarrier", new { RouteId = (int?)null, CarrierId = carrierID, UserId = userID, TotalRouteRateCarrierId = totalRouteRateCarrierId }, commandType: CommandType.StoredProcedure);

你在使用正则null时遇到的问题很可能是编译器无法推断 RouteId的类型。在匿名类型中仅使用 null没有 Actor 。

关于dapper - 如何使用 Dapper 传递空参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11106825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com