You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8中CS0118错误:'Customer'是命名空间却被当作类型使用

解决CS0118 'Customer' 是命名空间却被当作类型使用的问题

这个错误的核心原因是:你的Web项目里存在BulkyBook.Web.Areas.Customer命名空间(对应Customer区域),同时模型项目里又有同名的Customer实体类,编译器无法区分你代码中引用的是哪个。

给你几个直接可行的解决办法:

  • 使用完全限定名引用实体类
    在Controller代码里,直接写出Customer实体的完整命名空间,比如:

    // 声明变量时用全命名空间
    private readonly BulkyBook.Models.Models.Customer _customerEntity;
    

    或者在文件顶部添加别名,简化后续引用:

    // 在using区域添加别名
    using CustomerModel = BulkyBook.Models.Models.Customer;
    
    // 后续代码用别名指代实体类
    private readonly CustomerModel _customer;
    
  • 修改区域的命名空间(彻底避免冲突)
    把Customer区域的命名空间改成复数形式BulkyBook.Web.Areas.Customers,同步修改以下内容:

    • 把Area的文件夹名从Customer重命名为Customers
    • 调整Controller的命名空间为BulkyBook.Web.Areas.Customers.Controllers
    • 更新Controller上的[Area]特性参数为"Customers"
  • 清理错误的using语句
    检查Controller文件顶部的using列表,如果你不小心导入了BulkyBook.Web.Areas.Customer,编译器会优先把Customer识别为命名空间。移除这个多余的using,就能让编译器正确识别实体类了。

内容的提问来源于stack exchange,提问作者Nguyen Tuan Anh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 17:53:15