'Der Zugriff auf "" ist aufgrund des Schutzgrads nicht möglich.

  • C# (ASP)
  • (Core) MVC

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von mitrozo.

    'Der Zugriff auf "" ist aufgrund des Schutzgrads nicht möglich.

    Liebe Community,

    ich versuche mir grade ASP.NET Core beizubringen und komme grade nicht weiter, Fehler in meinem Controller lautet:

    'Der Zugriff auf "ApplicationDbContext.ProductTypes" ist aufgrund des Schutzgrads nicht möglich.

    Hier der Code:

    C#-Quellcode

    1. using BuchShop.Data;
    2. using Microsoft.AspNetCore.Mvc;
    3. using BuchShop.Models;
    4. namespace BuchShop.Areas.Admin.Controllers
    5. {
    6. [Area("Admin")]
    7. public class ProductTypesController : Controller
    8. {
    9. private ApplicationDbContext _db;
    10. public ProductTypesController(ApplicationDbContext db)
    11. {
    12. _db = db;
    13. }
    14. public IActionResult Index()
    15. {
    16. return View(_db.ProductTypes.ToList());
    17. }
    18. }
    19. }



    hier noch das Model:

    C#-Quellcode

    1. using System.ComponentModel.DataAnnotations;
    2. namespace BuchShop.Models
    3. {
    4. public class ProductTypes
    5. {
    6. public int Id { get; set; }
    7. [Required]
    8. [Display(Name ="Produkt Typ")]
    9. public string ProductTyp { get; set; }
    10. }
    11. }


    und hier der Context:

    C#-Quellcode

    1. using BuchShop.Areas.Admin.Controllers;
    2. using BuchShop.Models;
    3. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    4. using Microsoft.EntityFrameworkCore;
    5. namespace BuchShop.Data
    6. {
    7. public class ApplicationDbContext : IdentityDbContext
    8. {
    9. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    10. : base(options)
    11. {
    12. }
    13. DbSet<ProductTypes> ProductTypes { get; set; }
    14. }
    15. }


    leider verstehe ich diesen Fehler nicht. Vielleicht gibt es jemand der mir den Fehler erklären kann.


    Liebe Grüße und dank im Voraus
    Hallo,
    dein Property ProductTypes in deinem DB-Kontext hat keinen Zugriffsmodifizierer. Wenn keiner angegeben ist, wird private verwendet.

    Noch ein Tip: Auch wenn in einigen Tutorials bei MS es so zeigen, es ist keine gute Praxis, den DB-Kontext direkt in einen Controller zu übergeben. Schau mal hier.