ASP.NET MVC 代码首先相关 table 空引用
ASP.NET MVC code first related table null reference
我尝试从数据库中获取用户列表,但有时有 1 个相关 table 我称它为空并且对某些用户没有价值,所以我得到一个空引用错误。
@if (@row.UserProfile.DoctorDetail.Id != null) {
@row.UserProfile.DoctorDetail.Expertise
}
ASP.NET MVC returns 这个错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
那么如何检查相关 table 是否存在空引用?
我找到了答案,我正在将我的代码编辑为
@if (@row.UserProfile.DoctorDetail != null)
{
@row.UserProfile.DoctorDetail.Expertise
}
修正错误
我尝试从数据库中获取用户列表,但有时有 1 个相关 table 我称它为空并且对某些用户没有价值,所以我得到一个空引用错误。
@if (@row.UserProfile.DoctorDetail.Id != null) {
@row.UserProfile.DoctorDetail.Expertise
}
ASP.NET MVC returns 这个错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
那么如何检查相关 table 是否存在空引用?
我找到了答案,我正在将我的代码编辑为
@if (@row.UserProfile.DoctorDetail != null)
{
@row.UserProfile.DoctorDetail.Expertise
}
修正错误