Optional Parameters with EF Core FromSql

房东的猫 提交于 2019-12-01 23:13:43

In order to skip the optional parameters, you should use the named parameter syntax

@parameterName = parameterValue

as explained in the Specifying Parameter Names section of the SQL Server documentation for executing stored procedures.

Once you do that, there is no need to deal with DBNull and SqlParameters at all - you can use the FromSql overload accepting C# interpolated string and let EF Core create parameters for you.

Thus the sample code for calling the SP can be reduced to:

var result = context.Address.FromSql(
    $"exec CRM.TestNullableParameters @addressId = {201}, @createdByUserId = {1620}")
   .ToList();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!