dreevo
dreevo

Reputation: 29

Getting all rows in a entity framework table

I have a model called privilege requests :

public class PrivilegeRequests
    {
        public string UserEmail { get; set; }
        public string AdminEmail { get; set; }
    }

I've created a Db Context of this model :

public class PrivilegeRequestsDbContext : DbContext
    {
        public PrivilegeRequestsDbContext(DbContextOptions options) : base(options)
        {
        }

        DbSet<PrivilegeRequests> Requests
        {
            get; set;
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PrivilegeRequests>()
                .HasKey(c => new { c.UserEmail, c.AdminEmail });
        }
    }

I want to get all the rows in this table in my controller. I've already initialized the context in the controller :

private PrivilegeRequestsDbContext _context;
        public AdministrationController(RoleManager<IdentityRole> roleManager,UserManager<IdentityUser> userManager, PrivilegeRequestsDbContext context)
        {
            this.roleManager = roleManager;
            this.userManager = userManager;
            _context = context;
        }

The problem is that , in the action when I type _context. i can't access Requests table so I don't know where the issue is at.

Upvotes: 1

Views: 69

Answers (1)

Guru Stron
Guru Stron

Reputation: 143403

You need to declare Requests as public to access them via _context.Requests:

public class PrivilegeRequestsDbContext : DbContext
{
     ....

    public DbSet<PrivilegeRequests> Requests
    {
        get; set;
    }
}

Default access modifier for class members is private:

Class and struct members, including nested classes and structs, have private access by default. Private nested types aren't accessible from outside the containing type.

Upvotes: 1

Related Questions