0

When trying to print out all my users stored in UDbTable I get the error named in the title. It builds well until the moment I try to open the index that should contain the list.

What am I doing wrong?

Here's my code so far:

public class AdminController : BaseController
{
    private UserContext context;
    // GET: Admin
    [AdminMod]
    public ActionResult Index()
    {
        SessionStatus();
        if ((string)System.Web.HttpContext.Current.Session["LoginStatus"] != "login")
        {
            return RedirectToAction("Index", "Login");
        }
        var user = System.Web.HttpContext.Current.GetMySessionObject();
        UserData u = new UserData
        {
            Username = user.Username,
            Level = user.Level,
        };


        List<UDbTable> users = context.Users.ToList(); //program dies here
        return View("Index", users);
    }
}

Here's my UserContext:

public class UserContext : DbContext
    {
        public UserContext() :
            base("name=WebApplication1")
        {

        }

        public virtual DbSet<UDbTable> Users { get; set; }
    }

Here's my UDbTable:

public class UDbTable
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Username")]
    [StringLength(30, MinimumLength = 5, ErrorMessage = "Username cannot be longer than 30 char")]
    public string Username { get; set; }

    [Required]
    [Display(Name = "Password")]
    [StringLength(50, MinimumLength = 8, ErrorMessage = "Password short, >8 char")]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Email Address")]
    [StringLength(30)]
    public string Email { get; set; }

    [DataType(DataType.Date)]
    public DateTime LastLogin { get; set; }

    [StringLength(30)]
    public string LasIp { get; set; }

    public URole Level { get; set; }

}
dapet
  • 59
  • 7
  • _context_ is just declared. But this doesn't make it usable until you tell the compiler to build an instance of UserContext and assign that instance to _context_ It is a basic problem that needs to be fully understood. – Steve Apr 22 '22 at 20:12

0 Answers0