简体   繁体   中英

Namespace not found, even though it is declared in another file

I'm using Razor pages and asp.net on vscode.
I'm getting a cs0234 "The type or namespace name 'Data' does not exist in the namespace 'RazorPagesDoughnuts'"

This is the offending code in program.cs:

using RazorPagesDoughnuts.Services;
using RazorPagesDoughnuts.Data;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddTransient<JsonFileDoughnutService>();

builder.Services.AddDbContext<ApplicationDBContext>(options => options.UseSqlServer(
    builder.Configuration.GetConnectionString("DefaultConnection")
));
...

I have the declaration of the namespace as is shown below in another file:

using Microsoft.EntityFrameworkCore;
using RazorPagesDoughnuts.Models;

namespace RazorPagesDoughnuts.Data;

public class ApplicationDBContext : DbContext {

    public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
    {
        
    }

    public DbSet<Doughnut>? DoughnutDB { get; set; }
}
...

I'm not sure what the problem is? I've tried looking for similar errors, and they all seem to be related to the package version consistency. I'm not sure what to check though.

I think I fixed the error. A simple mistake on my part. I did not add the.cs file extension to the file containing the.Data namespace. I'm guessing this caused it to not be compiled and was therefore unavailable to other files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM