2012-06-19 119 views
26

デフォルト値のないパラメータを持つストアドプロシージャがありますが、nullでもかまいません。しかし、私はDapperでnullを渡す方法を理解できません。私はADOでうまくやることができます。Dapperでnullパラメータを渡す方法

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

例外:

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: 

は、ルートIDをオフに残すことはできません、私にそれが必要だと言うエラーが発生します。通常のnullを使用できない場合は、別のエラーが表示されます。ストアドプロシージャを変更することはできませんが、それは私のものではありません。

+0

RouteIdはどのタイプですか? – driis

+0

intです。あなたが何かを言う前に、覚えておいて、これをADOで例外なく行うことができます。 –

答えて

47

nullを適切なタイプにキャスティングすることで対応できるはずです。

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

定期的にヌルを使用しているときに発生している問題は、単にキャストせずにnullを使用した場合、コンパイラは、匿名型にRouteIdの種類を推測することができない可能性がある:のは、ルートIDが整数であると仮定しましょう。

+0

が完璧です。どうもありがとうございます!この場所はmsdnよりも良い方法です –

+0

あなたが助けてくれれば回答を受け入れることを忘れないでください:-) – driis

+0

私が受け入れるには10分待たなければなりません。あなたが新しい時にだけ投票しないようにすべきであるように思えます。愚かな –

関連する問題