C# – Namespace not recognized (even though it is there)

creference

I am getting this error:

The type or namespace name 'AutoMapper' could not be found (are you missing a using directive or an assembly reference?)

The funny thing is that I have that reference in my project already:

ProjectThatFails

And this is my code:

using System.Collections.Generic;
using DataContract;
using SelectorDAL;
using AutoMapper;

namespace SpecimenSelect
{
    public class SpecimenSelect : ISpecimenSelect
    {
        public SpecimenSelect()
        {
            SetupMaps();
        }

        private static void SetupMaps()
        {
            Mapper.CreateMap<SpecimenDetail, SpecimenDetailContract>();
        }

The other weird thing is that I have two other projects in my solution that both use AutoMapper and are referencing the exact same AutoMapper.dll file. They both work perfectly fine.

Here is a screen shot of one:

ProjectThatWorks

and here is that code (that compiles fine):

using System.Collections.Generic;
using AutoMapper;
using DataContract;
using SelectorDAL;

namespace PatientSelect
{

    public class PatientSelect : IPatientSelect
    {
        public PatientSelect()
        {
            SetupMaps();
        }

        private void SetupMaps()
        {
            Mapper.CreateMap<Patient, PatientContract>();
            Mapper.CreateMap<OrderedTest, OrderedTestsContract>();
            Mapper.CreateMap<Gender, GenderContract>();
        }

Both references seem to have the same data on the properties page.

What am I missing?

I tried:

  1. Restarting Visual Studio
  2. Referencing without a using statement (ie AutoMapper.Mapper.CreateMap)
  3. Clean and Rebuild

Any other ideas?

Best Answer

Check to make sure that your project isn't set up to use the .NET Framework 4 Client Profile.

You can check/change this by right-clicking your project (not the solution), select Properties -> Application -> Target framework. The target framework is a dropdown on that page.

This is a problem in Visual Studio (I would even go so far as to call it a bug). AutoMapper requires assemblies that are excluded from the .NET Framework 4 Client Profile. Since your project is using that version of the framework it breaks.

A similar error will propagate to the build process when the .NET Framework version for the project you are referencing is higher than the project making the reference. i.e. A project targeting 4.5 that references a project targeting 4.5.1 will give you this same error.

There needs to be a better error message when this happens because there is no rational explanation as to why it would not build as the error message tells you to reference an assembly you have clearly referenced.