I'm trying to add a list to the dto, in order for the dto top be mapped to an actual context class using AutoMapper, for some reason when i add the List to the dto's attribute and map it, upon debugging it throws an error with the exception:
The object reference is set to null.
DevDto.cs: (This is the Dto):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Auth.Models;
namespace Auth.Dtos
{
public class DevDto
{
public byte Id { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
public string Email { get; set; }
public ICollection<Tags> Tags { get; set; }
}
}
DevController (Class where the dto is trying to map to the Developer class):
public IHttpActionResult SaveDev(DevDto dev)
{
List<Tags> tags = new List<Tags>();
foreach(var ss in dev)
{
tags.Add(_context.Tag.Single(m => m.Id == ss.TagId));
}
foreach(var tt in tags)
{
dev.Tags.Add(tt);
}
var de = Mapper.Map<DevDto, Developers>(dev);
_context.Developer.Add(de);
Note: The Developer and Tag class have a one to many relationship respectively as the Developer class has Tag defined as:
public Tags Tag{get; set;}
Similarly the Tag class has Developer defined as:
public ICollection<Developer> Developer {get; set;}