0

This is my base class:

public class TaskBaseModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public DateTime? CurrentEndDate { get; set; }
    public int StatusId { get; set; }
    public TaskStatusModel Status { get; set; }      
    public string Priority { get; set; }
    public bool Milestone { get; set; }
    public string Type { get; set; }
    public string CreatorId { get; set; }
    public ApplicationUser Creator { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }            
    public bool IsDeleted { get; set; }
    public ICollection<TaskAssigneeModel> TaskAssignees { get; set; }        
    public ICollection<FavouriteTaskModel> FavouriteTasks { get; set; }
    public ICollection<TaskFeedModel> TaskFeedMessages { get; set; }
    public ICollection<TaskDependencyModel> TaskDependencies { get; set; }
    public ICollection<TaskDependencyModel> DependentTasks { get; set; }
    public ICollection<FileUploadModel> UploadedFiles { get; set; }
    public int ProjectId { get; set; }
    public ProjectModel Project { get; set; }

    //Not in DB
    public int NestedMilestonesCount { get; set; }
    public List<TaskDependencyModel> Dependencies { get; set; }
    public List<TaskDependencyModel> Dependents { get; set; }
    public List<string> AssigneesId { get; set; }
    public List<TreeItemModel> Path { get; set; }
    public bool HasComments { get; set; }
}

this is the class Im trying to map to:

public class ActivityModel : TaskBaseModel
{
    
    public int ModuleId { get; set; }
    public ModuleModel Module { get; set; }        
}

This is the map creation:

CreateMap<TaskBaseModel, ActivityModel>()
            .ReverseMap();

I have done other mappings in this code and works fine, I call this mapping like this:

var activity = Mapper.Map<ActivityModel>(taskModel);

And here always throws nullReferenceException.

I even tried to ignore properties in an attempt to discover which was coming throwing the exception, I ended up ignoring all like this: (and keeps throwing)

CreateMap<TaskBaseModel, ActivityModel>()                                                                                                                       
            .ForMember(x => x.AssigneesId, opt => opt.Ignore())
            .ForMember(x => x.CreatedAt, opt => opt.Ignore())
            .ForMember(x => x.Creator, opt => opt.Ignore())
            .ForMember(x => x.CreatorId, opt => opt.Ignore())
            .ForMember(x => x.CurrentEndDate, opt => opt.Ignore())
            .ForMember(x => x.Dependencies, opt => opt.Ignore())
            .ForMember(x => x.Dependents, opt => opt.Ignore())
            .ForMember(x => x.DependentTasks, opt => opt.Ignore())
            .ForMember(x => x.Description, opt => opt.Ignore())
            .ForMember(x => x.EndDate, opt => opt.Ignore())
            .ForMember(x => x.FavouriteTasks, opt => opt.Ignore())
            .ForMember(x => x.HasComments, opt => opt.Ignore())
            .ForMember(x => x.Id, opt => opt.Ignore())
            .ForMember(x => x.IsDeleted, opt => opt.Ignore())
            .ForMember(x => x.Milestone, opt => opt.Ignore())
            .ForMember(x => x.Module, opt => opt.Ignore())
            .ForMember(x => x.ModuleId, opt => opt.Ignore())
            .ForMember(x => x.Name, opt => opt.Ignore())
            .ForMember(x => x.NestedMilestonesCount, opt => opt.Ignore())
            .ForMember(x => x.Path, opt => opt.Ignore())                    
            .ForMember(x => x.Priority, opt => opt.Ignore())
            .ForMember(x => x.Project, opt => opt.Ignore())
            .ForMember(x => x.ProjectId, opt => opt.Ignore())
            .ForMember(x => x.StartDate, opt => opt.Ignore())
            .ForMember(x => x.Status, opt => opt.Ignore())
            .ForMember(x => x.StatusId, opt => opt.Ignore())
            .ForMember(x => x.TaskAssignees, opt => opt.Ignore())
            .ForMember(x => x.TaskDependencies, opt => opt.Ignore())
            .ForMember(x => x.TaskFeedMessages, opt => opt.Ignore())
            .ForMember(x => x.Type, opt => opt.Ignore())
            .ForMember(x => x.UpdatedAt, opt => opt.Ignore())
            .ForMember(x => x.UploadedFiles, opt => opt.Ignore())
            .ReverseMap();  

Can anyone help me on this?

Regards

MarchalPT
  • 1,268
  • 9
  • 28

1 Answers1

0

Try new'ing up Your collections with defaults for instance

//From
public ICollection<TaskAssigneeModel> TaskAssignees { get; set; }  

//To
public ICollection<TaskAssigneeModel> TaskAssignees { get; set; }  = new List<TskAssigneeModel>()

won't hurt your terribly if data actually exist when deserializing and it very often is not initialized collections I find when AutoMapper acts up I find, I recommend to give that a try first and the cost of assigning an empty collection. won't hurt much enough to measure unless you have really frequent operations.

T. Nielsen
  • 835
  • 5
  • 18
  • I just did that to try and see if the exception isn't thrown and it didnt work, keeps throwing.. – MarchalPT Nov 05 '21 at 12:50
  • Oh i see it got closed, wonder if it was you. So in order to do meaningful assistance I will suggest you take a payload which is failing and save it to a file, then do a unit test which does only this activity and the comment your your properties one by one until it works and you will have the culprit, without the data that causes fail it is hard to be specific – T. Nielsen Nov 05 '21 at 15:56