WTM
WTM copied to clipboard
项目文件中开启Nullable时会报错无法将空值转为字符串
看到新生成项目文件中TargetFramework为net6.0,觉得可以使用一些新特性了,就加了<Nullable>enable</Nullable>,结果在debug模式下,点击登录报如下错误:System.AggregateException: One or more errors occurred. (An error occurred while reading a database value for property 'FrameworkUser.Address'. The expected type was 'System.String' but the actual value was null.)
---> System.InvalidOperationException: An error occurred while reading a database value for property 'FrameworkUser.Address'. The expected type was 'System.String' but the actual value was null.
---> System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.
at MySqlConnector.MySqlDataReader.GetString(Int32 ordinal) in /_/src/MySqlConnector/MySqlDataReader.cs:line 283
at lambda_method70(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
经排查发现错误是由Nullable配置引起的,当他的值为enable,annotations时会报上面的错,值为disable,warnings时不报错。对了,我用的数据库是mysql