英文:
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; }
}
}
错误截图:
英文:
Need help to figure out the issue.
MovieContext.cs
file:
using Microsoft.EntityFrameworkCore;
namespace AspNetCoreWebApi6.Models
{
public class MovieContext : DbContext
{
public MovieContext(DbContextOptions<MovieContext> options)
:base(options)
{
}
DbSet<Movie> Movies { get; set; }
}
}
MoviesController.cs file
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 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:
答案1
得分: 1
你必须更改保护级别
public DbSet<Movie> Movies { get; set; }
英文:
You must Change Protection Level
public DbSet<Movie> Movies { get; set; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论