简体   繁体   English

在using语句中无法识别命名空间。 “名称空间'#'中不存在类型或名称空间'#'。是否缺少程序集引用?

[英]Namespace not recognised in using statement. "The type or namespace '#' does not exist in the namespace '#'. Are you missing an assembly reference?

I have a solution with about 14 projects, and am having a problem with a link I am trying to make between two of the projects: 我有大约14个项目的解决方案,并且在两个项目之间尝试建立链接时遇到问题:

  • Argus.Web (a web application project) Argus.Web(一个Web应用程序项目)
  • Argus.Domain.Office.Command (a project that reads data from an API and stores data in a local SQL Server database) Argus.Domain.Office.Command(一个从API读取数据并将数据存储在本地SQL Server数据库中的项目)
  • (there is also a project called Argus.Domain which I mention as it may possibly be part of the problem, although it hasn't caused problems in other similar places in my code.) (还有一个名为Argus.Domain的项目,尽管它并没有在我的代码中的其他类似地方引起问题,但我提到它可能是问题的一部分。)

Argus.Domain.Office.Command has a context (argusOfficeContext) linking it to a database which I would like to access from Argus.Web. Argus.Domain.Office.Command有一个上下文(argusOfficeContext)将其链接到我想从Argus.Web访问的数据库。 (This context works well in other situations.) When I try to add a using directive to reference the context's namespace in a class within Argus.Web I get the error in the title. (此上下文在其他情况下也能很好地工作。)当我尝试在Argus.Web的类中添加using指令来引用上下文的名称空间时,出现标题错误。

Here is the class I am scaffolding up (in Argus.Web) where the error occurs: 这是我正在搭建的类(在Argus.Web中),发生错误:

using Argus.Domain.Office.Command.Models;

namespace Argus.Web
{ 
    public partial class Calculator
    {
    }
}

The red wiggly line appears only under 'Office' in the using, and the error in full is "The type or namespace 'Office' does not exist in the namespace 'Argus.Domain'. Are you missing an assembly reference?". 红色的摆动行仅在使用中出现在“ Office”下,并且完全错误是“名称空间'Argus.Domain'中不存在类型或名称空间'Office'。是否缺少程序集引用?”。

I have checked that the project Argus.Domain.Office.Command is referenced from Argus.Web and when I check Argus.Web build dependencies I see Argus.Domain.Office.Command (as well as the other project Argus.Domain), and it is set to build before Argus.Web. 我检查了是否从Argus.Web引用了项目Argus.Domain.Office.Command,当我检查Argus.Web构建依赖项时,我看到了Argus.Domain.Office.Command(以及其他项目Argus.Domain),并且它设置为在Argus.Web之前构建。 When I check in Argus.Web References > Projects the project Argus.Domain.Office.Command has a tick next to it and I conclude that it is therefore referenced. 当我在Argus.Web参考>项目中签入项目时,Argus.Domain.Office.Command项目旁边有一个勾号,因此得出结论,因此已引用该项目。

Following Peter D's comment I checked the namespace declaration, and it is declared in the project Argus.Domain.Office.Command. 根据Peter D的评论,我检查了名称空间声明,该声明在项目Argus.Domain.Office.Command中进行了声明。

(Probably irrelevant:) here is the class I am trying to reference (可能不相关:)这是我要参考的课程

using System.Data.Entity;
using Argus.Domain.Office.Command.Models.Mapping;

namespace Argus.Domain.Office.Command.Models
{
    public partial class argusOfficeContext : DbContext
    {
        static argusOfficeContext()
        {
            Database.SetInitializer<argusOfficeContext>(null);
        }

        public argusOfficeContext()
            : base("Name=argusOfficeContext")
        {
        }

        public DbSet<Location> Locations { get; set; }
        public DbSet<LocationPeriodReport> LocationPeriodReports { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new LocationMap());
            modelBuilder.Configurations.Add(new LocationPeriodReportMap());
        }
    }
}

Both projects target .NET Framework 4.5.1, and I have looked for the answer in various places, but the fixes for the other questions below don't seem to fix my case. 这两个项目都针对.NET Framework 4.5.1,我在各个地方都在寻找答案,但是以下其他问题的修复似乎并不能解决我的问题。 I'm running Visual Studio 2015. Other questions for reference: 我正在运行Visual Studio2015。供参考的其他问题:

If anyone can advise what I'm doing wrong I would really appreciate it. 如果有人能告诉我我在做什么错,我将非常感激。 I have checked lots of other posts looking for an answer, but if you can provide a link to an answer I have missed that would also be great. 我检查了许多其他帖子以寻找答案,但是如果您可以提供指向答案的链接,我会错过那也很好。

Most of the times I see this, the cause is a class with the same name as part of the namespace. 在大多数情况下,我看到的原因是与名称空间相同名称的类。 These are in two different assemblies. 它们在两个不同的程序集中。

namespace Library.Foo // assembly 1
{
  using Library.Foo.Bar; // error here

  public class Bar
  {
    public void DoIt()
    {
      new Bell(); // and here
    }
  }
}

namespace Library.Foo.Bar // assembly 2
{
    public class Bell
    {
    }
}

This can be a real pita to find. 这可能是一个真正的皮塔饼。 I suggest doing a test search of each word in your namespace. 我建议对名称空间中的每个单词进行测试搜索。 You're looking for a class of the same name. 您正在寻找同名的课程。

As a side (and there's controversy over this), you can avoid some of these collisions by moving the using inside the namesapce . 一方面(对此存在争议),您可以通过在名称空间中移动use来避免某些冲突。

OK, so I came up with a solution. 好,所以我想出了一个解决方案。

I thought I would try removing and recreating the references, and in a nutshell, this worked. 我以为我会尝试删除并重新创建引用,并且总的来说,这可行。 Detail: 详情:

I right clicked on Argus.Web in the solution explorer, and then Build Dependencies > Project Dependencies. 我右键单击解决方案资源管理器中的Argus.Web,然后单击“构建依赖关系”>“项目依赖关系”。 I tried deselecting Argus.Domain.Office.Command, but got the error 'This dependency was added by the project system and cannot be removed'. 我尝试取消选择Argus.Domain.Office.Command,但收到错误消息“此依赖项已由项目系统添加,无法删除”。 I'm not sure why this error appears, but I therefore tried right clicking on the references in Argus.Web, and selected 'Add Reference'. 我不确定为什么会出现此错误,但是因此我尝试右键单击Argus.Web中的引用,然后选择“添加引用”。 In the Projects section of the window that came up I was able to deselect Argus.Domain.Office.Command, so I did this, commented out the problematic using directive, cleaned and rebuilt my solution, added the reference back (right clicking on the references in Argus.Web, and selecting 'Add Reference'), and it all works again. 在出现的窗口的“项目”部分中,我可以取消选择Argus.Domain.Office.Command,因此我这样做了,注释掉了有问题的using指令,清理并重建了我的解决方案,并添加了引用(右键单击在Argus.Web中引用,然后选择“添加引用”),则所有这些都可以再次使用。

I guess that the original references must have been misconfigured, but at present I can't see how this came about. 我想原来的引用一定是配置错误的,但是目前我还看不到这是怎么回事。 I will add to this post if I work out how this happened. 如果我弄清楚这是怎么发生的,我将添加到这篇文章中。

Many thanks to all respondees, all comments and answers were useful. 非常感谢所有受访者,所有评论和回答都很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型或名称空间名称在名称空间中不存在“是否缺少程序集引用?” - The type or namespace name does not exist in the namespace ''are you missing an assembly reference ?" 命名空间中不存在类型或命名空间名称(您是否缺少程序集引用?) - The type or namespace name does not exist in the namespace (are you missing an assembly reference?) CS0234:命名空间“<namespace1>”中不存在类型或命名空间名称“<namespace2>”。 (你错过了一个程序集引用吗?) - CS0234: The type or namespace name '<namespace2>' does not exist in the namespace '<namespace1>.' (are you missing an assembly reference?) 命名空间中不存在类型或命名空间名称(是否缺少程序集引用?)- 无法引用 class - The type or namespace name does not exist in the namespace (are you missing an assembly reference?) - Can't reference a class 错误 CS0234:命名空间“Microsoft”中不存在类型或命名空间名称“Azure”(您是否缺少程序集引用?) - error CS0234: The type or namespace name 'Azure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 命名空间“System.Web”中不存在类型或命名空间名称“Mvc”(您是否缺少程序集引用?)Kentico - The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) Kentico 命名空间“Microsoft.SqlServer”中不存在类型或命名空间名称“Smo”(您是否缺少程序集引用?) - The type or namespace name 'Smo' does not exist in the namespace 'Microsoft.SqlServer' (are you missing an assembly reference?) 命名空间“MicrosoftSqlServer”中不存在类型或命名空间名称“Management”,您是否缺少程序集引用 - the type or namespace name 'Management' does not exist in the namespace 'MicrosoftSqlServer' are you missing an assembly reference CS0234 C#类型或名称空间名称&#39;Slydeextender&#39;在名称空间中不存在(您是否缺少程序集引用?) - CS0234 C# The type or namespace name 'Slydeextender' does not exist in the namespace (are you missing an assembly reference?) 命名空间“Android.Support.V4.App”中不存在类型或命名空间名称“TaskStackBuilder”(您是否缺少程序集引用?) - The type or namespace name 'TaskStackBuilder' does not exist in the namespace 'Android.Support.V4.App' (are you missing an assembly reference?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM