title: Why is my DTO Null?
body: <p>I have a few DTOs in a project I am working on. I am using AutoMapper to create the mappings. All of the mappings work except for one of them. I can tell because when retrieving data using LINQ Method Syntax, I am getting Null references. Anyway here is all the code I believe is relevant:</p>
  
  <p>MappingProfile.cs</p>
  
  <pre><code>using AutoMapper;
  using GigHub.Controllers.Api;
  using GigHub.Dtos;
  using GigHub.Models;
  
  namespace GigHub.App_Start
  {
      public class MappingProfile : Profile
      {
          public MappingProfile()
          {
              Mapper.CreateMap&lt;ApplicationUser, UserDto&gt;();
              Mapper.CreateMap&lt;Gig, GigDto&gt;();
              Mapper.CreateMap&lt;Notification, NotificationDto&gt;();
              Mapper.CreateMap&lt;Following, FollowingDto&gt;();
          }
      }
  }
  </code></pre>
  
  <p>Global.asax.cs</p>
  
  <pre><code>using AutoMapper;
  using GigHub.App_Start;
  using System.Web.Http;
  using System.Web.Mvc;
  using System.Web.Optimization;
  using System.Web.Routing;
  
  namespace GigHub
  {
      public class MvcApplication : System.Web.HttpApplication
      {
          protected void Application_Start()
          {
              Mapper.Initialize(c =&gt; c.AddProfile&lt;MappingProfile&gt;());
              GlobalConfiguration.Configure(WebApiConfig.Register);
              AreaRegistration.RegisterAllAreas();
              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);
              BundleConfig.RegisterBundles(BundleTable.Bundles);
          }
          public static void RegisterRoutes(RouteCollection routes)
          {
              routes.IgnoreRoute("elmah.axd");
  
          }
  
      }
  }
  </code></pre>
  
  <p>Following.cs</p>
  
  <pre><code>using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace GigHub.Models
  {
      // Alternatively, this class could be called Relationship.
      public class Following
      {
          [Key]
          [Column(Order = 1)]
          public string FollowerId { get; set; }
  
          [Key]
          [Column(Order = 2)]
          public string FolloweeId { get; set; }
  
          public ApplicationUser Follower { get; set; }
          public ApplicationUser Followee { get; set; }
      }
  }
  </code></pre>
  
  <p>FollowingDto.cs</p>
  
  <pre><code>namespace GigHub.Dtos
  {
      public class FollowingDto
      {
          public string FolloweeId { get; set; }    
      }
  }
  </code></pre>
  
  <p>FollowingsController.cs</p>
  
  <pre><code>using System.Linq;
  using System.Web.Http;
  using GigHub.Dtos;
  using GigHub.Models;
  using Microsoft.AspNet.Identity;
  
  namespace GigHub.Controllers.Api
  {
      [Authorize]
      public class FollowingsController : ApiController
      {
          private ApplicationDbContext _context;
  
          public FollowingsController()
          {
              _context = new ApplicationDbContext();    
          }
              //CheckFollow is what I am using to test the Dto
          [HttpGet]
          public bool CheckFollow(FollowingDto dto)
          {
              var userId = User.Identity.GetUserId();
              if (_context.Followings.Any(f =&gt; f.FolloweeId == userId &amp;&amp; f.FolloweeId == dto.FolloweeId))
                  return true;
              else
                  return false;
  
          }
  
          [HttpPost]
          public IHttpActionResult Follow(FollowingDto dto)
          {
              var userId = User.Identity.GetUserId();
  
              if (_context.Followings.Any(f =&gt; f.FolloweeId == userId &amp;&amp; f.FolloweeId == dto.FolloweeId))
                  return BadRequest("Following already exists.");
  
              var following = new Following
              {
                  FollowerId = userId,
                  FolloweeId = dto.FolloweeId
              };
              _context.Followings.Add(following);
              _context.SaveChanges();
  
              return Ok();
          }
      }
  }
  </code></pre>
  
  <p>WebApiConfig.cs</p>
  
  <pre><code>using Newtonsoft.Json;
  using Newtonsoft.Json.Serialization;
  using System.Web.Http;
  
  namespace GigHub
  {
      public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
              var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
              settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
              settings.Formatting = Formatting.Indented;
  
              config.MapHttpAttributeRoutes();
  
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{action}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
          }
      }
  }
  </code></pre>
  
  <p>I doubt the tool matters, but I've been using Postman to test my APIs. The only difference I see in this Dto from the others is that the column that Automapper will create the mapping for has the Key and Column Order attributes in the model. I don't understand why that would matter though. I've searched online about this issue already and the solutions didn't help since I am already doing them. Can anyone tell from the code I posted why I can be getting a Null reference? Technically the error in Postman says System.Exception.TargetException but from what I understand it means your LINQ query isn't returning any results.</p>
  
  <p>I know my query works as I've passed in a string FolloweeId that was in my db and the API CheckFollow action worked. So I can only assume that my Dto isn't getting mapped correctly. </p>