MovieContext.Movies’ is inaccessible due to its protection level error in ASP.NET Core 6 Web API

huangapple go评论82阅读模式
英文:

MovieContext.Movies' is inaccessible due to its protection level error in ASP.NET Core 6 Web API

问题

需要帮助解决问题。

MovieContext.cs 文件:

using Microsoft.EntityFrameworkCore;
namespace AspNetCoreWebApi6.Models
{
    public class MovieContext : DbContext
    {
        public MovieContext(DbContextOptions<MovieContext> options)
            : base(options)
        {
        }
        DbSet<Movie> Movies { get; set; }
    }
}

MoviesController.cs 文件

using AspNetCoreWebApi6.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AspNetCoreWebApi6.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MoviesController : ControllerBase
    {
        private readonly MovieContext _dbContext;

        public MoviesController(MovieContext dbContext)
        {
            _dbContext = dbContext;
        }

        // Get api/Movies
        public async Task<ActionResult<IEnumerable<Movie>>> GetMovies()
        {

            if (_dbContext.Movies == null)
            {
                return NotFound();
            }
            return await _dbContext.Movies.ToListAsync();
        }

    }
}

Models/Movie.cs 文件

namespace AspNetCoreWebApi6.Models
{
    public class Movie
    {
        public int Id { get; set; }
        public string? Title { get; set; }
        public string Genre { get; set; }
        public DateTime? ReleaseDate { get; set; }
    }
}

错误截图:

MovieContext.Movies’ is inaccessible due to its protection level error in ASP.NET Core 6 Web API

英文:

Need help to figure out the issue.

MovieContext.cs file:

using Microsoft.EntityFrameworkCore;
namespace AspNetCoreWebApi6.Models
{
	public class MovieContext : DbContext
	{
		public MovieContext(DbContextOptions&lt;MovieContext&gt; options)
			:base(options)
		{
		}
		DbSet&lt;Movie&gt; Movies { get; set; }
	}
}

MoviesController.cs file

using AspNetCoreWebApi6.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AspNetCoreWebApi6.Controllers
{
	[Route(&quot;api/[controller]&quot;)]
	[ApiController]
	public class MoviesController : ControllerBase
	{
		private readonly MovieContext _dbContext;

		public MoviesController(MovieContext dbContext)
		{
			_dbContext = dbContext;
		}

		//Get api/Movies
		public async Task&lt;ActionResult&lt;IEnumerable&lt;Movie&gt;&gt;&gt; GetMovies() {

			if (_dbContext.Movies == null) { 
				return NotFound();
			}
			return await _dbContext.Movies.ToListAsync();
		}

	}
}

Models/Movie.cs file

namespace AspNetCoreWebApi6.Models
{
	public class Movie
	{
		public int Id { get; set; }
		public string? Title { get; set; }
		public string Genre { get; set; }
		public DateTime? ReleaseDate { get; set; }
	}
}

Error screenshot:

MovieContext.Movies’ is inaccessible due to its protection level error in ASP.NET Core 6 Web API

答案1

得分: 1

你必须更改保护级别

public DbSet<Movie> Movies { get; set; }
英文:

You must Change Protection Level

public DbSet&lt;Movie&gt; Movies { get; set; }

huangapple
  • 本文由 发表于 2023年5月28日 04:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定