I am working on a ASP.NET Core MVC Application with .NET Core 2.1.200.
I have a response model and a static method to build this response model from the entity model.
public static EntityTypeResponseModel FromEntityType(Entity.EntityType entityType)
{
return new EntityTypeResponseModel
{
Id = entityType.Id,
Name = entityType.Name,
// NullReferenceException
Fields = entityType.EntityTypeFields?.Select(x => FieldResponseModel.FromField(x.Field))
};
}
Although I use null propagation a NullReferenceException is thrown.
Doing a traditional null check fixes the issue:
public static EntityTypeResponseModel FromEntityType(Entity.EntityType entityType)
{
var entityTypeResponseModel = new EntityTypeResponseModel
{
Id = entityType.Id,
Name = entityType.Name
};
if (entityType.EntityTypeFields != null)
{
entityTypeResponseModel.Fields =
entityType.EntityTypeFields?.Select(x => FieldResponseModel.FromField(x.Field));
}
return entityTypeResponseModel;
}
Am I missing something? Is this a bug?