我正在使用Entity Framework对SQL存储过程执行NonQuery。
它抱怨的属性对于此记录确实为null,但是我的存储过程将其定义为可以为空的参数。
为什么抱怨这个?
SQL Proc定义:
答案 0 :(得分:0)
斯坦利在评论中有正确答案。
您无法将null传递给SqlParameter
,您需要传递DbNull.Value
。
所以:
object comments = detail.ReferralComments;
if (comments == null) {
comments = DBNull.Value;
} else {
comments = detail.ReferralComments;
}
db.Database.ExecuteSqlCommand("EXEC dbo.Dual_SaveReferral @quoteGuid, @referralTypeID, @comments",
new SqlParameter("@quoteGuid", detail.QuoteGuid),
new SqlParameter("@referralTypeID", detail.ReferralTypeID),
new SqlParameter("@comments", comments));
}