Compiler Error CS0118 Namespace Used As A Type

Brian Ashcraft 100 Reputation points
2023-02-14T14:27:56.01+00:00

Hello,

I am using ASP .Net Core 6, in Visual Studio 2022 (Community).

I have created the following classes.

There is a 1:1 Relationship between KbaStatus and Kba.

A Kba can have a single KbaStatus.

using System.ComponentModel.DataAnnotations;

namespace DT6.Models
{
    public class KbaStatus
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Description")]
        [StringLength(25, ErrorMessage = "Description cannot be longer than 25 characters.")]
        public string Description { get; set; }

        public Kba Kba { get; set; }
    }
}
using System.ComponentModel.DataAnnotations;

namespace DT6.Models
{
    public class Kba
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Title")]
        [StringLength(100, ErrorMessage = "Title cannot be longer than 100 characters.")]
        public string Title { get; set; }

        [Required]
        [Display(Name = "Description")]
        [StringLength(150, ErrorMessage = "Description cannot be longer than 150 characters.")]
        public string Description { get; set; }

        [Required]
        [Display(Name = "Narrative")]
        [StringLength(5000, ErrorMessage = "Narrative cannot be longer than 5000 characters.")]
        public string Narrative { get; set; }

        //Each Kba can have one status
        [Required]
        public KbaStatus KbaStatus { get; set; }
        public int KbaStatusId { get; set; }

    }
}

In my ApplicationDbContext.cs I have the following:

public DbSet<Kba> Kba { get; set; }
public DbSet<KbaStatus> KbaStatus { get; set; }

When creating the Indes.cshtml.cs for KbaStatus I am getting a CS0118 compiler error. 'KbaStatus' is a namespace but is used like a type.

using DT6.DataAccess.Data;
using DT6.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DT6_Web.Pages.Admin.KbaStatus
{
    [Authorize(Roles = SD.AdminEndUser)]
    public class IndexModel : PageModel
    {
        private readonly ApplicationDbContext _context;

        public IndexModel(ApplicationDbContext context)
        {
            _context = context;
        }

        //ERROR HERE
        public IList<KbaStatus> KbaStatus { get; set; }

    }
}

Note that if I change the IList as below, the compiler error is not displayed. Visual Studio does not prompt me to add 'using DT6.Models'; to the top of the page. It is not an option in the lightbulb dropdown menu. Manually adding the 'using' statement to the top of the page does not fix the issue either. The only fix is to add 'DT6.Models' to the IList.

public IList<DT6.Models.KbaStatus> KbaStatus { get; set; }

I have obviously overlooked something, but cannot figure out what.

I did a global search of the entire Solution and found no instances of KbaStatus being declared as a Namespace.

Any guidance is appreciated.

Thanks!

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | Visual Studio | Debugging
Developer technologies | Visual Studio | Other
Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-02-14T15:07:45.5933333+00:00

    In your code, KbaStatus is also a namespace: namespace DT6_Web.Pages.Admin.KbaStatus.

    Maybe rename it, for example: namespace DT6_Web.Pages.Admin. Then add the corresponding using directives, if required.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.