Namespace not recognized (even though it is there)

前端 未结 20 1688
终归单人心
终归单人心 2020-11-28 21:27

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?)

相关标签:
20条回答
  • 2020-11-28 22:03

    I've had a similar issue, that took a bit to troubleshoot, so I thought to share it:

    The namespace that could not be resolved in my case was Company.Project.Common.Models.EF. I had added a file in a new Company.Project.BusinessLogic.Common namespace.

    The majority of the files were having a

    using Company.Project;
    

    And then referencing the models as Common.Models.EF. All the files that also had a

    using Company.Project.BusinessLogic;
    

    Were failing as VS could not determine which namespace to use.

    The solution has been to change the second namespace to Company.Project.BusinessLogic.CommonServices

    0 讨论(0)
  • 2020-11-28 22:03

    Acknowledging this as an older post but still figured I'd add this suggestion to the list of responses since I hadn't seen it mentioned.

    If the unresolved reference exists within context of another Project in the Solution:

    Right-click the Project having the problem, select Build Dependencies --> Project Dependencies and ensure the desired Project is selected for reference.

    I had the same issue as described in the post however none of the suggested manners for resolution worked. I'd never seen such a quirk in VS before (running VS 2019 at present)

    I checked for potential namespace issues and a plethora of the more obvious causes and nothing made sense. Intellisense even acknowledged the existence of the other Project and suggested to a the using statement but even after having added the using reference; VS 2019 still wouldn't acknowledge the other project.

    Forcing the dependency in the manner described resolved the issue.

    0 讨论(0)
  • 2020-11-28 22:04

    This has to be the simplest solution if all the other answers does not help you

    I was searching for what's wrong with my setup among the answers, Tried all of them - none worked, Then I realized Visual Studio 2018 was developed by Microsoft. So I did what most people do,

    Restarted Visual Studio And It worked

    0 讨论(0)
  • 2020-11-28 22:06

    If your class does not compile, even if it is in the project check these:

    1. whether class name is exactly the same
    2. whether name space is exactly the same
    3. whether class properties show build action = compile
    0 讨论(0)
  • 2020-11-28 22:07

    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.

    0 讨论(0)
  • 2020-11-28 22:07

    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?

    0 讨论(0)
提交回复
热议问题