2017-10-13 92 views
1

我試圖重構代碼使用dbContext.Database.ExecuteSqlCommandAsync,而不是SqlCommandExec的實體框架中的核心存儲過程的輸出參數2

List<SqlParameter> parameters = new List<SqlParameter>(); 

parameters.Add(new SqlParameter("@certificateTypeId", "TA")); 
parameters.Add(new SqlParameter("@personId", 1m)); 
parameters.Add(new SqlParameter("@selfAssessment", xDocument.ToString())); 
parameters.Add(new SqlParameter("@selfAssessmentVersion", 1m)); 
parameters.Add(new SqlParameter("@sponsorPersonId", 1m)); 

var outputParam = new SqlParameter("@certificationEventId", SqlDbType.Decimal, 10); 
outputParam.Direction = ParameterDirection.Output; 
parameters.Add(outputParam); 

string sql = "[My Stored Proc] " + string.Join(" ", parameters.Select(ToEfString).ToArray()); 

// runtime value: [My Stored Proc] @certificateTypeId @personId @selfAssessment @selfAssessmentVersion @sponsorPersonId @certificationEventId OUT 

var result = await dbContext.Database.ExecuteSqlCommandAsync(sql, parameters); 

return ((SqlDecimal)outputParam.SqlValue).Value; 

我得到這個錯誤:

SqlException: Incorrect syntax near @personId

什麼問題?

+0

嘗試增加逗號之間:'[我的存儲過程] @certificateTypeId,@personId,@ ...' –

+0

可以指定存儲過程參數名稱顯式在SQL調用? –

+0

@BernardVanderBeken:我不確定你的意思 – Liero

回答

3

您需要添加逗號的參數之間:

[My Stored Proc] @certificateTypeId, @personId, @... 
相關問題