Namespace not recognized (even though it is there) [duplicate]
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:
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:
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:
- Restarting Visual Studio
- Referencing without a using statement (ie
AutoMapper.Mapper.CreateMap
) - Clean and Rebuild
Any other ideas?
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.
Let me ask a stupid question: Could there be two automapper.dll files? One with an AutoMapper
namespace and one without? Confirm the paths in both projects.
I also noticed that the order of the using
commands is different. It shouldn't matter, but have you tried to shuffle them?