数据库中的某字段值为: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.CompanyStreetAddress
,CompanyCity
,CompanyZipCode
,CompanyVatNumber
,ContactFirstName
,ContactLastName
- all these are marked as[Required]
in the model, but have no correspondingnot null
constraint in the table.
原来是实体类中的该字段有[Required]属性,所以抛出了错误,去掉该属性,OK。