What I am trying to do is create a function that will pass through the id of the user and then will load up a page that displays all the applications that a user has made to each driving instructor.
I have already made a function that will display all applications from the database regardless of who created the application. I have also made a function that allows users to create applications. I have also created a field in the application table named User_UserId so when an application is created and commited to a database, it will add the specific userId that created the application.
My question is, how would I make it so that when you clicked on show all applications on a specific users account, it would only pull up applications that have that User_UserId attached. Could I add UserId to the class that stores the input form and set the UserId in there to the UserId that is creating the application?
My attempts so far all end up with this error in the User Controller on line - IList<Application> applications = user.Applications.ToList();:
An exception of type 'System.ObjectDisposedException' occurred in EntityFramework.dll but was not handled in user code
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I have a function that allows me to view applications by Driving instructors and that works fine, it uses a similar method where it asks for the specific driving instructor in the create form. Then I have a function that lists the application by driving instructor.
Here is the code. Some main points to make are;
- Primary key UserId in user class is set as a string
- In the class that stores the input data, there is no UserId field as that is passed in the method.
- The field in Applications table that stores the user Id is named User_UserId
Please inform me if there is any more pieces of code you require.
User Controller that opens the view for GetApplications:
public ActionResult GetApplications(string id)
{
User user = userService.GetUser(id);
IList<Application> applications = user.Applications.ToList();
return View(applications);
}
IDAO/DAO Files:
IList<Application> GetApplications(string id, XContext context);
public IList<Application> GetApplications(string id, XContext context)
{
User user = context.Users.Find(id);
return user.Applications.ToList();
}
IService/Service Files:
IList<Application> GetApplications(string id);
public IList<Application> GetApplications(string id)
{
using (var context = new XContext())
{
IList<Application> Applications;
Applications = userDAO.GetApplications(id, context);
return Applications;
}
}
HTML view for GetUser that leads to GetApplications, Model uses User class:
@Html.ActionLink("Show Applications", "GetApplications", "User") ||
Context class:
public class XContext : DbContext
{
public XContext() : base("XContext")
{
Database.SetInitializer(new XInitializer());
}
public DbSet<User> Users { get; set; }
public DbSet<Application> Applications { get; set; }
public DbSet<Instructor> Instructors { get; set; }
}
UserService GetUser function:
public User GetUser(string id)
{
using (var context = new XContext())
{
return userDAO.GetUser(id, context);
}
}
UserDAO GetUser function:
public User GetUser(string id, XContext context)
{
return context.Users.Find(id);
}
User Class:
using System.ComponentModel.DataAnnotations;
public class User
{
[Key]
public string UserId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public virtual ICollection<Application> Applications { get; set; }
public virtual ICollection<Instructor> Instructors { get; set; }
}
Application Class:
public class Application
{
[Key]
public int ApplicationId { get; set; }
public string Level { get; set; }
public string Manual { get; set; }
public string InstructorName { get; set; }
}
I have set breakpoints on the controller function that breaks and here are the results:
User user = userService.GetUser(id);
id = "1"
user = null
IList<Application> applications = user.Applications.ToList();
id = "1"
applications = null
When I continue and get the error thrown, I am given
id = "1"
applications = null
user.Application = null
question from:
https://stackoverflow.com/questions/65941731/asp-net-mvc-objectdisposedexception-when-trying-to-pass-id