我正在.NET Core应用程序中处理Entity Framework Core 2.1。我已经编写了存储过程以通过Context Query-Type获取声明列表,该声明采用三个参数,其中两个为可空Guid。只要没有传递空参数,它都能正常工作,但传递空参数,则会引发错误
https://docs.microsoft.com/en-us/ef/core/modeling/query-types
The parameterized query '(@UserVal uniqueidentifier,@ClientVal nvarchar(4000),@Consultati' expects the parameter '@ClientVal', which was not supplied.
public override IQueryable<GetSystemClaims> Execute()
{
public Guid UserId { get; set; }
public Guid? ClientId { get; set; }
public Guid? ConsultationId { get; set; }
var userParam = new SqlParameter("@UserVal", UserId);
var clientParam = new SqlParameter("@ClientVal", ClientId);
var consultationParam = new SqlParameter("@ConsultationVal", ConsultationId);
var userClaimsList = Context.Query<UserClaimsQueryView>().FromSql("EXECUTE dbo.ListUserClaims @userId=@UserVal, @clientId=@ClientVal, @consultationId=@ConsultationVal"
, userParam, clientParam, consultationParam);
return userClaimsList;
}
答案 0 :(得分:3)
var userParam = new SqlParameter("@UserVal", UserId);
var clientParam = new SqlParameter("@ClientVal", SqlDbType.UniqueIdentifier);
clientParam.Value = (object)ClientId ?? DBNull.Value;
var consultationParam = new SqlParameter("@ConsultationVal", SqlDbType.UniqueIdentifier);
consultationParam.Value = (object)ConsultationId ?? DBNull.Value;
var userClaimsList = Context.Query<UserClaimsQueryView>().FromSql("EXECUTE dbo.ListUserClaims @userId=@UserVal, @clientId=@ClientVal, @consultationId=@ConsultationVal"
, userParam, clientParam, consultationParam);
答案 1 :(得分:1)
您也可以按照以下方式进行操作。
如果您想在值中允许空值,则可以如下所示:
var countryParam = new SqlParameter("@CountryName", searchField.CountryName);
countryParam.Value = (object)searchField.CountryName ?? DBNull.Value;