SqlBuffer.get_String() Data is Null

阅读 1

数据库中的某字段值为:NULL,以前可以读出数据,在最近的一次改动中,抛出了如下错误:

System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
   at Microsoft.Data.SqlClient.SqlBuffer.get_String()

排查了半天,有点莫名其妙,最后在 stackoverflow 上找到了答案,原文如下:

Looking at your entity model and corresponding database table, you can see the obvious discrepancy for many string properties -> varchar columns. CompanyStreetAddressCompanyCityCompanyZipCodeCompanyVatNumberContactFirstNameContactLastName - all these are marked as [Required] in the model, but have no corresponding not null constraint in the table.

原来是实体类中的该字段有[Required]属性,所以抛出了错误,去掉该属性,OK。

各位看官,如果你觉得本文不错,请支持一下~