Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

c# - ASP.NET MVC ObjectDisposedException when trying to pass id

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your Application class is incorrect. It doesn't have UserId

public class Application
{
    [Key]
    public int ApplicationId { get; set; }
    public string UserId { get; set; }
    public string Level { get; set; }
    public string Manual { get; set; }
    public string InstructorName { get; set; }
}

And I think you have the same bug in your Instructor class

You have the bugs in your DAO.

Replace

    public User GetUser(string id, XContext context)
    {
        return context.Users.Find(id);
    }

with

    public User GetUser(string id, XContext context)
    {
        return context.Users.Include(i=> i.Applications).FirstOrDefault(i=>i.UserId==id);
    }

And replace

        public IList<Application> GetApplications(string id, XContext context)
    {
        User user = context.Users.Find(id);
        return user.Applications.ToList();
    }

with

    public IList<Application> GetApplications(string id, XContext context)
    {
        return context.Applications.Where(i=>i.UserId==id).ToList();
    }

User service code:

public List<Application>  GetApplications(string id)
{
    using (var context = new XContext())
    {
        return userDAO.GetApplications(id, context);
    }
}


Your controller code:


public ActionResult GetApplications(string id)
    {
        var applications = userService.GetApplications(id);
        return View(applications);
    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...