2015-11-10 58 views
0

我正在使用實體框架針對SQL存儲過程執行NonQuery。實體框架 - 參數不提供?

它抱怨的屬性對於這條記錄確實爲null,但是我的存儲過程將其定義爲一個可爲空的參數。

爲什麼它會抱怨呢?

Error

SQL PROC定義:

SQL Stored Procedure

+2

我無法看到的截圖,但你通常設置參數值時使用'DBNull.Value',而不是'null'。 –

+0

修復它,謝謝。我創建了一個對象,如果它爲null,則將對象設置爲DbNull.Value,否則將其設置爲屬性值。 – Patrick

回答

0

d士丹利的意見正確答案。

您不能將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)); 
}